Variable Length Arguments

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 135:- Variable Length Arguments

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.

16. JavaScript - Functions and Array

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support