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
  • 19,490
  • 9
  • 75
  • 99
user2301515
  • 4,639
  • 6
  • 27
  • 40

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
  • 959,406
  • 173
  • 1,780
  • 1,769