Reputation: 195
var a = 2;
var b = a;
console.log( b ); //2
a= 5;
console.log( b ); //2
Q: Why variable 'b' is getting value 2 even when variable 'a' is assigned a different value
Upvotes: 2
Views: 1115
Reputation: 350077
Others may tell you it has to do with immutibility, but that's not true. Even if you run the example with mutable values, it is the same behaviour:
var a = [2];
var b = a;
console.log( b ); // [2]
a = [5];
console.log( b ); // [2]
This behaviour is not explained with immutability, but with the difference between assignment and mutation. Each variable has its own memory location1, so when two variables happen to have the same value, this value is present twice (In case of an object, this means both variables have copies of the reference to that object). When you assign to one of those two variables, the other is unaffected.
However, when two variables have a reference to the same object, and you mutate that object (by assigning to properties, not the variables), then of course both variables will still reference that same, single object (as their values/references didn't change). The object has mutated:
var a = [2];
var b = a;
console.log( b ); // [2]
a[0] = 5; // We don't assign to `a`, but to the array index (property)!
console.log( b ); // [5]
But mutability has to do with property assignment (the object to which the property belongs will mutate), not with variable assignment.
1 there are exceptions to this rule, like with arguments
in sloppy mode.
Upvotes: 0
Reputation: 2340
When doing Assignment of primitive values in javascript:
It's important to point out that this assignment does not tie a
and b
together. In fact all that happened was that the value from a
was copied into b
, so when we go to change a
we don't have to worry about affecting b
. This is because the two variables are backed by two distinct memory locations – with no crossover.
In brief way:
When you assign b = a
Actually you didn't copy the reference of a
variable and make b
point to the same variable location in memory.
You only copy the value of a
variable and put it in new variable b
with different memory location.
Upvotes: 1
Reputation: 1035
In Javascript, integers are immutable
. It means that the object's value once assigned cannot change. When you do
a=5;
b=a;
It is true that both are names of the same object whose value is 5
.
Later when you do -
a=2
It assigns the reference a
a new object whose value is 2. So essentially a
now points to a new object. Ans both objects exist.
For a better understanding you can refer to this link
Upvotes: 1
Reputation: 574
In javascript, primitives (number, bool, string) are assigned by value, only objects are assigned by reference.
Upvotes: 1
Reputation: 1600
console.log(b)
returns 2
because when you access a primitive type you work directly on its value.
Upvotes: 2
Reputation: 705
Cause numbers are immutable.
Changing an immutable value, replaces the original value with a new value, hence the original value is not changed (thats why b = 2
).
If you need a reference, use object and/or arrays
var a ={value: 2}, b = a
a.value = 3 // also changes the value of be, since it can mutate
Upvotes: 1