0

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?

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
user2301515
  • 4,903
  • 6
  • 30
  • 46

1 Answers1

2

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875