Skip to main content

Default parameters

@serverSerrverlesskiy

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.

Teacher

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​

book

Live Editor
Result
Loading...

Passing other "false" values​

basketball

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​

Math

In the default parameters, you can use the values of the previous (located to the left in the list) parameters:

Live Editor
Result
Loading...

An example of a function with and without default parameters πŸ‘‡:

Live Editor
Result
Loading...

The result will be the same, but without default parameters, the codeπŸ“Ÿ can become noticeably larger.

Problems?​

Problem

Write to Discord chat.

Questions:​

Question

If no value is passed to function parametersβš™οΈ, what is their default value?

  1. null
  2. undefined
  3. NaN

Are the default parameters "catching" false values?

  1. Yes
  2. No

Is it possible to use the values of the parameters located to the left of the list in the default parameters?

  1. Yes
  2. 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.

JS Camp

  1. MDN web docs

Contributors βœ¨β€‹

Thanks goes to these wonderful people (emoji key):


Philipp Dvinyaninov

πŸ“–

Dmitriy Vasilev

πŸ’΅

Resoner2005

πŸ› 🎨 πŸ–‹

Navernoss

πŸ–‹ πŸ› 🎨

Become a Patron!