Friday, 14 December 2018

JavaScript: Rest Parameter

Rest parameters are used to represent variable number of arguments. It is equivalent to Varargs in Java. ‘…’ are used to represent variable number of arguments.

HelloWorld.js
function sum(...arr){
    var sum = 0;
    
    for(data of arr){
        sum += data;
    }
    
    return sum;
}

var x = sum(10, 20);
var y = sum(10, 20, 30, 40);

console.log("x : " + x);
console.log("y : " + y);

Output
x : 30
y : 100


Previous                                                 Next                                                 Home

No comments:

Post a Comment