Hello all, I just wonder is it possible to write a function in JavaScript without have any parameter in describe paranthesis but actually using them inside. For example in below, the function would give me 10 but if I want to change my paramenter number to 5 and want to calculate sumThem(1,2,3,4,5);
this function should give me 15 as result. Thank you😊
function sumThem() { //No parameters will be there
//let result = some magic here... result become sum of all parameters.
return result;
}
console.log(sumThem(1,2,3,4));
Yes. You can declare a variable inside a function, and you often will.
If you are supposed to sum all of the arguments passed to your function, then you should read about the arguments object which will give you access to an array-like structure containing all the arguments passed to the function.
1 Like
I think I misunderstood your original question. I didn’t realize that you were asking about using arguments inside a function without putting them in the function signature. I thought you were just asking about variables in general.
For what you were really asking, check out the arguments object:
1 Like