0

I want to know, in JavaScript, if is possible to change value of the variable within another variable.

var a;
a= 0;
b= a;
b= 1;

alert(a);

Why output of var a is 0 ? How can I have like output 1 ?

if b == a why when i change value of b doesn't change even value of var a ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Borja
  • 3,359
  • 7
  • 33
  • 66
  • 2
    Not possible. https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – epascarello Mar 19 '18 at 17:08
  • 1
    when you do `b = a` the *value* of a is copied into b. They dont refer to the same memory location. – Jonas Wilms Mar 19 '18 at 17:09
  • 1
    Primitive types are passed by value in JavaScript. You would be looking for passing by reference. Here's a discussion on the topic. https://stackoverflow.com/questions/1686990/javascript-variable-reference-alias – adprocas Mar 19 '18 at 17:09
  • 1
    Possible duplicate of https://stackoverflow.com/questions/34344365/multiple-references-to-a-variable-in-javascript – Reck Mar 19 '18 at 17:11

0 Answers0