Default parameters
Default parameters allow you to set default values for function parameters if the function is called without arguments, or if the parameter is explicitly passed the value undefined
.
In JavaScript, function parameters⚙️ that are not passed values when called are defaulted to undefined
. However, in some cases it may be useful to set a different default value. This is where the default options are intended.
Syntax
Passing other "false" values
If any value other than undefined
is passed to the formal parameter during the call, including one of the" false "values such as false , 0
, " "
, ''
, null
,NaN
, then in this case the default value will not be assigned to the parameter. In this case, you need to write the code yourself that will catch these "false values".
Examples of
In the default parameters, you can use the values of the previous (located to the left in the list) parameters:
An example of a function with and without default parameters :
The result will be the same, but without default parameters, the code can become noticeably larger.
Problems?
Write to Discord chat.
Questions:
If no value is passed to function parameters⚙️, what is their default value?
null
undefined
NaN
Are the default parameters "catching" false values?
- Yes
- No
Is it possible to use the values of the parameters located to the left of the list in the default parameters?
- Yes
- No
In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.
Links
Contributors ✨
Thanks goes to these wonderful people (emoji key):
Philipp Dvinyaninov | Dmitriy Vasilev 💵 | Resoner2005 🐛 🎨 🖋 | Navernoss 🖋 🐛 🎨 |