softare development

Accept Unlimited Arguments in a JavaScript Function

In this tutorial, we will see how to accept unlimited arguments in a JavaScript function.

A function is block of code that can hold and process a logic. Functions in JavaScript can be declared using the function keyword or arrow function.

function sum(){
console.log("this is a function.");
}
sum();

Function With Parameters

Functions can also have parameters and the number of parameters supplied must be equal to the number of parameters passed.

function sum(a,b){
return a + b;
}
console.log(sum(5,5)); //10

Function With Unlimited Arguments

Functions can also take unlimited number of parameters as well. To accept an unlimited number of arguments in a JavaScript function, we will use the spread operator.

 function sum(...nums){
   let sum = nums.reduce((a, b) => {
     return a + b;
   })
   return sum
}
console.log(sum(4,5,6,2,3,3))

The above function will accept unlimited number of parameters in the JavaScript function. The function returns a sum of all the passed arguments.

Top 30 React Native Questions and Answers

Recent Posts

How To Migrate from PostgreSQL to MySQL

Database migration is one of the most challenging tasks in software engineering. While both PostgreSQL…

2 hours ago

Hidden Gems Inside Modern JavaScript

Modern JavaScript isn’t just let, const, arrow functions, and promises anymore. Over the years, the language has…

15 hours ago

Software Developer Pain Points Ranked: What Frustrates Developers the Most?

Software development is one of the most rewarding careers in technology, but it is also…

1 day ago

How to Print Documents in JavaScript

Printing a document in JavaScript usually means triggering the browser’s print dialog and controlling what…

3 days ago

CSS Display Cheatsheet

The display property controls how an element behaves in the layout and how its children are arranged. Access software…

1 week ago

10 JavaScript Habits Destroying Your Code

JavaScript is one of the most flexible programming languages ever created. That flexibility is powerful,…

1 week ago