+ 1

Number of parameters

Hi, if you declare 3 parameters when you name the function, can you then only use 2 or 1?

18th Nov 2020, 9:29 AM
Qwerty_GMR - avatar
2 Answers
+ 5
Yes, the remaining parameters will be set to undefined (which may cause errors depending on your function body). https://www.sololearn.com/learn/JavaScript/1147/ You may also want to look into default parameters and variadic parameters (variadic functions) or the spread operator etc.
18th Nov 2020, 9:41 AM
ChaoticDawg - avatar
+ 5
You can set default parameters, in case the user doesn't provide them to you, function add(a = 0, b = 0) { return a + b } add(5, 7) // 12 add() // 0 If i didn't set the default values for a and b in the function, and the user doesn't pass the values, they would become undefined, and it would return undefined + undefined which is equal to NaN (not a number)
18th Nov 2020, 12:05 PM
maf - avatar