-1

Why doesn't this work and is there any way to get it to work?

Im pretty sure it doesn't work because of variable scoping in javascript, but I can't think of any ways to get it to work.

Code

var x = 5;
var z = function(y) { y = 10; };
z(x);
console.log(x) // Outputs 5 instead of 10
Community
  • 1
  • 1
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109
  • 4
    I'm pretty sure this question has been asked many times, but the simple answer is you can't change the values of primitive types passed into a function. – Will Dec 21 '15 at 23:45
  • 1
    the only function called is `console.log`, so why would the value of `x` change in any way? – zzzzBov Dec 21 '15 at 23:45

4 Answers4

2
var x = 5
var z = function(y) { y = 10; }
console.log(x) // Outputs 5 instead of 10

Where exactly did you changed X? you dont have X and you did not call the function as well.

I assume you wish to do this:

var x = {x: 5}
var z = function(y) { y.x = 10; }
z(x);
console.log(x.x) // Outputs 10

The code is using x as variable on an object instead of a primitive number which is passed by value and not by reference and thus cannot be modified.

Using object you can modify the attributes (properties) of the object

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
1

X won't change for a couple reasons. The first is that you are modifying the parameter y inside of the function. If you passed an object this would work as those are passes by reference. It would also work if you changed x directly instead of passing it as an argument.

The second reason is that you never ran the function. You need to call z with z()

Marie
  • 2,114
  • 1
  • 17
  • 31
0

What happens when you invoke the function z is that the parameters get initialized and assigned the value you passed in.

So the function might read something like

var y = 5;
y = 10;

and so, the y that is in the scope outside of the function, remains unchanged because this y is actually a whole new y variable just for this function's scope.

0
var x = 5;
var z = function(y)
 {
 y = 10; 
 //assigned value 10 to the function parameter variable y.
//You haven't done x=y or x=10 and so how do you expect the value of x to change?
}
 console.log(x) // Outputs 5 instead of 10

Change it to:

var x = 5 ;
var z = function(y)
 {
 x = 10; //changed value of x to 10
}
 console.log(x) // Outputs 10

The problem is not related to vatiable scope at all.Your variable x is global since it is outside the function. AND YES,you can change it's value inside a function.But the problem with your code was that you never changed the value of x.

If this is what you intended to do:

z(x); //x=5 is passed to the function z
 function(y)
 {   
  y=10; //initially the value of y is 5.Since you have passed x.
  //Doing y=10 does not change the value of x because y is another variable.The value of x was copied to y and y is not x.

  x=y;//You have assigned 10 to x
  }
 console.log(x); //outputs 10

Changing the value of the function parameter does not change the value of the variable whose value you passed to the function.You are actually passing the value of x to y that is you are passing 5 to y and not the variable itself.

You are not passing the variable reference.You are just passing the value.

Mathews Mathai
  • 1,707
  • 13
  • 31