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

javascript rest

function sum(...numbers) {
    console.log(numbers); // [1, 2, 3]
    return numbers.reduce((a, b) => a + b, 0);
}

console.log(sum(1, 2, 3)); // 6
console.log(sum(3, 2));    // 5
console.log(sum());        // 0
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 :: package.json 
Javascript :: javascript most frequent in a list 
Javascript :: delete value from json array with index 
Javascript :: javascript Iterate Through Iterables 
Javascript :: express ubuntu ERR_CONNECTION_REFUSED 
Javascript :: back button not working when modal open in react native 
Javascript :: regex for fullstop 
Javascript :: use obj property inside itself 
Javascript :: calcular sobra de divisão de parcelas js 
Javascript :: how to add elements into an array in javascript 
Javascript :: javascript in pdf 
Javascript :: javascript to Postgress 
Javascript :: Fetching data error and pending load check 
Javascript :: javascript online compiler 
Javascript :: enable emmet in vscode for jsx 
Javascript :: javascript wait to execute function on keyup 
Javascript :: slice 
Javascript :: javascript detect paste contents 
Javascript :: make alphabet js 
Javascript :: angular firebase 
Javascript :: browser.find_element_by_ <a 
Javascript :: nodemon.json env 
Javascript :: capitalize first letter of a string 
Javascript :: empty the integer array javascript 
Javascript :: react navigation 
Javascript :: how to coerce a string to number in javascript 
Javascript :: add update react pwa feature 
Javascript :: useref in react hooks 
Javascript :: how to combine two regular expressions in javascript 
Javascript :: Get Arrays in sequelize 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =