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 :: codesandbox react emet 
Javascript :: localstorage in next js 
Javascript :: javascript string return character 
Javascript :: what is local storage and session storage in javascript 
Javascript :: react-scripts not found 
Javascript :: what is closures in javascript 
Javascript :: how to initialize an array in javascript 
Javascript :: anti aliasing 
Javascript :: parse query url javascript 
Javascript :: Set CSS styles with javascript 
Javascript :: click function in js 
Javascript :: remove duplicates array javascript 
Javascript :: age calculator moment js 
Javascript :: create slice redux 
Javascript :: nodejs debug 
Javascript :: sort an array 
Javascript :: how to use break in javascript 
Javascript :: scrollbar position 
Javascript :: passing data in route react 
Javascript :: jquery sweet popup 
Javascript :: upload file in node 
Javascript :: how to clear radio field in jquery 
Javascript :: how to break from map in javascript 
Javascript :: remove duplicate item from array javascript 
Javascript :: javascript string to ascii array 
Javascript :: types of method in js 
Javascript :: window viewport width 
Javascript :: Looping arrays with for loop 
Javascript :: nextjs use dotnenv 
Javascript :: read more/less button with smoth expand 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =