Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  153] [ 3]  / answers: 1 / hits: 27281  / 8 Years ago, thu, august 25, 2016, 12:00:00

Let's say I have this :



function concatenate(a, b, c) {
// Concatenate a, b, and c
}




How do I handle those calls ?



x = concatenate(a)

x = concatenate(a, b)

x = concatenate(a, c)




How can I make my function aware of the parameter I gave to it ?


More From » javascript

 Answers
67

Any unfilled argument will be undefined.



concatenate(a, c) is equivalent to concatenate(a, b). You cannot pass the third parameter without passing the second; but you can pass undefined (or null, I suppose) explicitly: concatenate(a, undefined, c).



In the function, you can check for undefined and replace with a default value.



Alternately, you can use an object argument to imitate keyword arguments: concatenate({a: a, c: c}).


[#60922] Tuesday, August 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rafael

Total Points: 5
Total Questions: 103
Total Answers: 103

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;