If i have code, where i want in console "0 1" instead of "0 0"
function increase(f) { f++; }
var n = 0;
console.log(n);
increase(n);
console.log(n);
How to increase in such way the variable. I wish use like in c-language &v. Is it possible?
If i have code, where i want in console "0 1" instead of "0 0"
function increase(f) { f++; }
var n = 0;
console.log(n);
increase(n);
console.log(n);
How to increase in such way the variable. I wish use like in c-language &v. Is it possible?
There is nothing increase
can do to the parameter f
that will affect the variable n
in your example.
When you do increase(n)
, the value of n
is passed into increase
. That value is not in any way linked to the variable n
.
(This is called "pass by value;" JavaScript is a purely pass-by-value language, more in this question's answers.)
Instead, have increase
return the new value, and assign it to n
.