Sunday, November 28, 2021

Rest parameter in javascript

Rest Parameter (...) : The rest parameter in JavaScript (introduced in ES6) allows a function to accept multiple arguments and collect them into a single array.

Syntax

function functionName(...args) {
// args is an array
}
function sum(...numbers) {
console.log(numbers);
}

sum(1, 2, 3, 4);

Output: [1, 2, 3, 4]

Rest parameter must be the last parameter in a function.

function test(...numbers, a) {
}

Invalid



No comments:

Post a Comment

Javascript module

JavaScript Modules (ES6) let you split code into multiple files and reuse code cleanly. Main keywords: export → make something available...