Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How Do Rest Parameters Work in JavaScript?

// 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.
Comment

rest parameters javascript

/*
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 
Comment

javascript rest parameter

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')
Comment

javascript Rest Parameter

let func = function(...args) {
    console.log(args);
}

func(3); // [3]
func(4, 5, 6); // [4, 5, 6]
Comment

rest parameters javascript

/*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
Comment

PREVIOUS NEXT
Code Example
Javascript :: fuzzy search javascript 
Javascript :: click function in js 
Javascript :: find function in javascript 
Javascript :: find duplicates array javascript 
Javascript :: change css variable with javascript 
Javascript :: turn string into number javascript 
Javascript :: how to get nested array using lodash 
Javascript :: javascript make do while loop 
Javascript :: Auto increment in realtime database with javascript 
Javascript :: nodejs debug 
Javascript :: best way to filter table in angular 
Javascript :: javascript exeit from loop 
Javascript :: javascript return data async 
Javascript :: set element at index javascript array and create new array 
Javascript :: javascript scrape page 
Javascript :: login js 
Javascript :: mdn javascript 
Javascript :: es6 hashset 
Javascript :: js role giver 
Javascript :: how to break from map in javascript 
Javascript :: trigger jquery autocomplete on click 
Javascript :: php math 
Javascript :: js var part of var name 
Javascript :: never give up 
Javascript :: javascript detect time on page 
Javascript :: Is Even 
Javascript :: javascript add nd to number 
Javascript :: sum in javascript 
Python :: python tkinter window fullscreen 
Python :: pandas save file to pickle 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =