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 :: how to test timeout in component did mount 
Javascript :: 4. You want to print the incremental count each time you instantiate a object using new in JS 
Javascript :: how to access viewmodel in jquery 
Javascript :: in object transform translate property concat with rotate value angular 7 
Javascript :: adding a terminal iframe 
Javascript :: show object unordered in chrome console 
Javascript :: what is @ atsign in first of file path nodejs 
Javascript :: populate DataTable from django json 
Javascript :: js remove first element from string 
Javascript :: how to set input of time type to current time on initialization 
Javascript :: how display same paragraph in all pages of website in js 
Javascript :: js extract all tags not have attr 
Javascript :: js var 
Javascript :: javascript stringify line breaks 
Javascript :: javascript check great 
Javascript :: how to use a script to inject a meta attribute in html 
Javascript :: use ca certifcate node js 
Javascript :: ipa failed react native after processing 
Javascript :: js create object url base64 pdf binary 
Javascript :: js run html in blob 
Javascript :: jlkjlkj 
Javascript :: {backgroundimage: `url("${require(`../../assets/images/${post.image}`)}")`}; 
Javascript :: plyr.js example in angular 10 
Javascript :: creating a read stream from a large text file 
Javascript :: handling props in functional components reactjs ijnterview questions 
Javascript :: symbols with object.assign 
Javascript :: es6 currying 
Javascript :: angular material nested tree 
Javascript :: 5.4.2. else Clauses¶ 
Javascript :: moment format escape 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =