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

Introduction to Phaser JS

Phaser JS is a powerful, open-source HTML5 game development framework used for creating 2D games that…

5 hours ago

Web Authentication Libraries

JavaScript / Node.js Authentication Libraries 1. Passport.js One of the most popular authentication middleware libraries…

5 hours ago

The Things They Carry: Software Developers Starter Packs

Every profession comes with its own set of tools. A carpenter has a toolbox, a…

6 hours ago

CRUD Operations: The Foundation of Data Management

Every application that stores and manages data relies on a set of basic operations known…

2 weeks ago

Common PHP Mistakes Every Developer Should Avoid

PHP remains one of the most widely used server-side programming languages, powering platforms such as…

2 weeks ago

Danfo.js: The JavaScript Data Science Library

Danfo.js is an open-source JavaScript library designed for data manipulation, analysis, and machine learning. It provides…

2 weeks ago