⏱ 0:00est. 7 min
Chapter 39- 'arguments' and spread
Notes
#BIGWORD Arguments - The parameters you pass to a function,javascript gives you a keyword contains list of all the values of all the parameters that you pass to a function.
Example 1
function greet(firstname, lastname, language){
console.log(firstname);
console.log(lastname);
console.log(language);
}
//this function can be invoked without being any params passed to it
greet();
OUTPUT
undefined
undefined
undefined
greet(‘John');
OUTPUT
John
undefined
undefined
greet(‘John','Doe');
OUTPUT
John
Doe
undefined
greet(‘John','Doe','es');
OUTPUT
John
Doe
es
Example 2 //setting up default value
function greet(firstname, lastname, language){
language = language || 'en';
console.log(firstname);
console.log(lastname);
console.log(language);
}
greet(‘John','Doe');
OUTPUT
John
Doe
en
New Keyword #ARGUMENTS
Example 3
function greet(firstname, lastname, language){
if(arguments.length === ‘0'){
console.log(‘Missing parameters');
return;
}
language = language || 'en';
console.log(firstname);
console.log(lastname);
console.log(language);
console.log(arguments);
}
greet(‘John','Doe');
OUTPUT
John
Doe
en
[‘John','Doe'] // containing all the values of params we have passed
Arguments - looks like an array, act like an array but not exactly the javascript array. It doesn't have all the features of the javascript array
SPREAD // using ...name (ES6)
//wrapping up all other arguments in ...other param
function greet(firstname, lastname, language, ...other){
if(arguments.length === ‘0'){
console.log(‘Missing parameters');
return;
}
language = language || 'en';
console.log(firstname);
console.log(lastname);
console.log(language);
console.log(arguments);
}
greet(‘John','Doe','es','111 main st','new york');