I am sure this is very simple but I can’t get seem to get this to work just right. Here is my code:
var zero = 10;
function some(elem, num) {
return elem += num;
}
console.log(some(zero, 10));
console.log(some(zero, 10));
console.log(some(zero, 10));
From what I understand, I was expecting the variable in zero to increase each time by 10.. So, it would be 10, 20 and 30. Instead, I keep getting 10. Obviously, this isn’t right.
My question is, how would I increase the variable zero by 10 each time and save that value back to the variable zero?
JavaScript passes by value, so modifying the passed value won’t have any effect on the caller’s variable. You’d need to use the return value: