Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

the rest operator javascript

function sum(...numbers) {
	return numbers.reduce((accumulator, current) => {
		return accumulator += current;
	});
};
 
sum(1,2) // 3
sum(1,2,3,4,5) // 15
Comment

How to Use the Rest Operator in JavaScript

// A perfect example to illustrate this would be if we have a list of numbers,
// and we want to use the first number as the multiplier.
// We then want to put the multiplied value of the remaining numbers in an array:

const multiplyArgs = (multiplier, ...otherArgs) => {
    return otherArgs.map((number) => {
    return number * multiplier;
    });
};

let multipiedArray = multiplyArgs(6, 5, 7, 9);

console.log(multipiedArray); // [30,42,54]


// Here is a good representation of the rest operator and what its value looks like:

const multiplyArgs = (multiplier, ...otherArgs) => {
    console.log(multiplier); // 6
    console.log(otherArgs); // [5,7,9]
};

multiplyArgs(6, 5, 7, 9);


// Note: The Rest parameter must be the last formal parameter.

const multiplyArgs = (multiplier, ...otherArgs, lastNumber) => {
    console.log(lastNumber); // Uncaught SyntaxError: Rest parameter must be last formal parameter
};

multiplyArgs(6, 5, 7, 9);
Comment

How to Use the Rest Operator in JavaScript

const func = (first, ...rest) => {};
Comment

how to assign an rest operator in javascript

function multiply(multiplier, ...theArgs) {
  return theArgs.map(element => {
    return multiplier * element
  })
}

let arr = multiply(2, 1, 2, 3)
console.log(arr)  // [2, 4, 6]
Comment

Rest Operator in javascript

function restOp(firstNum, ...args) {
    let result = [];
    for (let i in args) {
        const multi = firstNum * args[i];
        result.push(multi)
    }
    return result;
}
console.log(restOp(3, 2, 3, 5));
//Output:[ 6, 9, 15 ]
Comment

PREVIOUS NEXT
Code Example
Javascript :: call c# function from javascript 
Javascript :: hide react source 
Javascript :: what is let js 
Javascript :: get subdomain from url javascript 
Javascript :: javascript save multiple images to server 
Javascript :: is there an api for netflix shows 
Javascript :: toggle div javascript 
Javascript :: document.addeventlistener 
Javascript :: code cat 
Javascript :: difference between dom and react dom 
Javascript :: react map list render dictionary 
Javascript :: js find intersection point 
Javascript :: How to Define a Function using a Function Expression in javascript 
Javascript :: how does an if statement work 
Javascript :: js check for obj property 
Javascript :: how to interrupt scroll with jquery 
Javascript :: even numbers in an array 
Javascript :: javascript check string sort ascending 
Javascript :: how to count click events javascript 
Javascript :: using settings_data.json shopify 
Javascript :: emit event to a single socket id in socket 
Javascript :: why is my bot not going online discord.js 
Javascript :: "npm supertest 
Python :: python get public ip address 
Python :: uuid regex 
Python :: import validation error in django 
Python :: spinning donut python 
Python :: python pandas save df to xlsx file 
Python :: dataframe column to string 
Python :: python mkdir 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =