If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In JavaScript, variable length arguments are implemented using the rest parameter syntax. The rest parameter syntax allows you to represent an indefinite number of arguments as an array.
Here is an example of using rest parameter syntax to create a function that can accept a variable number of arguments:
javascriptCopy code
function sum(...args) { let result = 0; for (let i = 0; i < args.length; i++) { result += args[i]; } return result; } console.log(sum(1, 2)); // Output: 3 console.log(sum(1, 2, 3)); // Output: 6 console.log(sum(1, 2, 3, 4)); // Output: 10
In the example above, the ...args
syntax defines a rest parameter that captures any remaining arguments passed to the function as an array. The function then iterates over this array to calculate the sum of all arguments.
The rest parameter syntax can also be used in arrow functions, like so:
javascriptCopy code
const sum = (...args) => { let result = 0; for (let i = 0; i < args.length; i++) { result += args[i]; } return result; } console.log(sum(1, 2)); // Output: 3 console.log(sum(1, 2, 3)); // Output: 6 console.log(sum(1, 2, 3, 4)); // Output: 10
In this case, the ...args
syntax is used in the parameter list of the arrow function to define a rest parameter.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.