Created
November 12, 2016 07:26
-
-
Save Ajax30/d95d77241210e7e0c6a0b2beb47e5682 to your computer and use it in GitHub Desktop.
Add ALL the arguments passed to a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(){ | |
var sum = 0; | |
// Use "the arguments object" to add up all the arguments | |
// passed to a function | |
for (i=0; i<arguments.length; i++){ | |
var sum = sum + arguments[i]; | |
} | |
console.log(sum); | |
} | |
// You can now add as many numbers as you like | |
// just by passing them to the function | |
add(1, 2, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment