Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Variadic function in javascript

//Sum of variable number of Arguments
function sum() {
    let x = 0;

  // loop performs on number of arguments e.g. sum(1,2,3) i.e. three times
    for (let i = 0; i < arguments.length; ++i)
        // loop performs below equation 
  		// x += arguments[0]; x += arguments[1]; x += arguments[2]; 
        x += arguments[i];

    return x;
}

sum(1, 2); // returns 3
sum(1, 2, 3); // returns 6
sum(1, 2, 3, 4); // returns 10

Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery append method 
Javascript :: disable textarea angular 
Javascript :: string to query string javascript 
Javascript :: js similar jquery document ready 
Javascript :: form serialze submit button 
Javascript :: ternary operator js 
Javascript :: jquery get data attribute value by class 
Javascript :: capitalize first letter of a string 
Javascript :: iis express gzip 
Javascript :: switch into the postgres user windows 10 
Javascript :: jquery on method 
Javascript :: how to include a toasted property in vue 
Javascript :: js queryselectorall 
Javascript :: how to find dates in a string in js 
Javascript :: nested array in json 
Javascript :: add update react pwa feature 
Javascript :: copy svg to clipboard javascript 
Javascript :: comments in js 
Javascript :: chunking array javascript 
Javascript :: scroll down angular with animation 
Javascript :: filepond remove uploaded file 
Javascript :: react js if statement 
Javascript :: jquery scroll to element toggle menu item 
Javascript :: use effect hook 
Javascript :: react document viewer 
Javascript :: fetcher for swr 
Javascript :: vue js filter 
Javascript :: js array string includes 
Javascript :: create a component in react 
Javascript :: javascript sleep one second 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =