JavaScript - Why

Hi all. I’m on lesson 87 of the basics for javascript. I’m struggling to understand something.

var name = ‘Mary’;
name = ‘John’;

Why am I not just changing the ‘Mary’ to ‘John’ in the original declaration? Why go to all the trouble of adding another line of code? I’m sorry if this is a stupid question, I’m really grappling with the WHY of a lot of this stuff so far. I now find I’m completely stuck because I don 't understand this .
This question isn’t directly related to #87, that’s just where i happen to be.
Thanks in advance

You declare a variable, and assign it a value.

At some point later (in this case the next line, immediately, but it could be long after the initial declaration), you assign it another value.

var i = 0;

while (i < 5) {
  console.log("Looping...");
  i = i + 1;
}

There’s an example. The variable i is assigned the value 0. Then until the value of i reaches 5, log a thing and reassign (same as thing you’re confused about in the example)

1 Like