Say that we have this function called greeting and it expects a name parameter. When passing a value as an argument, the value of the name parameter will hold this value. Now let's see what happens if we forget to pass an argument at all. You can see that name now has the value of undefined.
The default value of arguments is the value undefined unless we pass a value. Luckily in ES6, we can create default parameters in order to avoid that the parameter has the value of undefined, which you probably don't want in most cases. We can set the argument value equal to the default value we prefer. In this case, I want the value of the name argument to have the value of Lydia, unless we pass our own value.
You can see that in the first one, the value of the name argument was the value that was passed to the function, so John. However, in the second one, when we didn't pass any value, the value of the name parameter out the default value, so Lydia in this case. This is a way to override the default value of undefined that normally parameters have. Let's create a createSuperhero function that receives a name argument and a health points argument.
By default, the superheroes get 100 health points, unless a different value is passed. In this case, Superwoman has more health points than Superman. The default argument is evaluated at call time. This means that Every time we call the function, a new object is created.
Let's say that we have a function called addListItem that receives an item and a list. The list has the default value of an empty array to which we can push the passed item. Now when we invoke the function twice with different items, a new array will be created since it's evaluated at call time. You can see that two new arrays have been created.
to which we push the item. You can also have functions as default parameters. Here you can see a function getDate that returns the formatted date with the formatted time. I want to have a logMessage function that takes a message argument and a prefix argument, which gives me more information about the log message.
I don't always want to have to pass a value for the prefix. So by default, the value of the prefix argument will be equal to the returned value of the getDate function. Now let's log a simple message, for example, I like turtles. You can see that the current time and date followed by the string appear in the console.
But we can also pass a value for the prefix argument. You can see the nail polish emoji as the prefix instead of the current date. This is handy when we want to debug our applications. As you can see, The default parameter is great if you don't want arguments to have the default value of undefined if you didn't provide a value.
Say that we have this function called greeting and it expects a name parameter. When passing a value as an argument, the value of the name parameter will hold this value. Now let's see what happens if we forget to pass an argument at all. You can see that name now has the value of undefined.
The default value of arguments is the value undefined unless we pass a value. Luckily in ES6, we can create default parameters in order to avoid that the parameter has the value of undefined, which you probably don't want in most cases. We can set the argument value equal to the default value we prefer. In this case, I want the value of the name argument to have the value of Lydia, unless we pass our own value.
You can see that in the first one, the value of the name argument was the value that was passed to the function, so John. However, in the second one, when we didn't pass any value, the value of the name parameter out the default value, so Lydia in this case. This is a way to override the default value of undefined that normally parameters have. Let's create a createSuperhero function that receives a name argument and a health points argument.
By default, the superheroes get 100 health points, unless a different value is passed. In this case, Superwoman has more health points than Superman. The default argument is evaluated at call time. This means that Every time we call the function, a new object is created.
Let's say that we have a function called addListItem that receives an item and a list. The list has the default value of an empty array to which we can push the passed item. Now when we invoke the function twice with different items, a new array will be created since it's evaluated at call time. You can see that two new arrays have been created.
to which we push the item. You can also have functions as default parameters. Here you can see a function getDate that returns the formatted date with the formatted time. I want to have a logMessage function that takes a message argument and a prefix argument, which gives me more information about the log message.
I don't always want to have to pass a value for the prefix. So by default, the value of the prefix argument will be equal to the returned value of the getDate function. Now let's log a simple message, for example, I like turtles. You can see that the current time and date followed by the string appear in the console.
But we can also pass a value for the prefix argument. You can see the nail polish emoji as the prefix instead of the current date. This is handy when we want to debug our applications. As you can see, The default parameter is great if you don't want arguments to have the default value of undefined if you didn't provide a value.