// With rest parameters, you can define a function to store multiple arguments in a single array
function sayHello(message, ...names){
names.forEach(name => console.log(`${message} ${name}`));
}
sayHello('Hello', "John", "Smith", "Doe");
/*
output:
Hello John
Hello Smith
Hello Doe
*/
// The ... is what makes names a rest parameter.
/*
That allows a function to accept an indefinite number of arguments as an array
*/
function fun(...input){
let sum = 0;
for(let i of input){
sum+=i;
}
return sum;
}
console.log(fun(1,2)); //3
console.log(fun(1,2,3)); //6
console.log(fun(1,2,3,4,5)); //15
function show(a, b, ...args) {
console.log(a); // one
console.log(b); // two
console.log(args); // ["three", "four", "five", "six"]
}
show('one', 'two', 'three', 'four', 'five', 'six')
let func = function(...args) {
console.log(args);
}
func(3); // [3]
func(4, 5, 6); // [4, 5, 6]
/*The rest parameter syntax allows a function to accept an indefinite
number of arguments as an array, */
function getFirstElementOfArray(...args){
return args[1]
}
getFirstElementOfArray("shirshak","Sabtech","Javascript")// return shirshak