So now that you know what variables are (kinda?), it is a good time for us to talk about types. In our previous example (where I disclosed where I live), we had Peleg who is a human and Charlie who is a dog. In that case, I guess, Peleg's type in computer terms would be considered "Human," whereas Charlie's type, "Dog." We use this distinction between types so we can give them different attributes and expect from them different behaviors. Consider this: In real life, you would expect Charlie, whose type is Dog to have four legs, but from Peleg, the human, you'd expect to have only two.
Now the following blurb is important to know so I decided to include it, but it may be a bit tough to grasp this early in your career as a programmer. Feel free to just index it in your magnificent brain and smile; it's not crucial to your programming abilities at this point.
NaN(Not a Number...)
These types are considered primitive in that they are immutable in their nature and cannot be changed. JS abstracts this stuff as well and allows us to change a string without having to care about the fact that it actually needs to duplicate the string in order for it to look like it's changed. So consider the following.
Let's declare a variable
someString and assign a string to it:
var someString = 'This is just a random string.';
In order to append something to that string we can do the following:
someString += ' And this is its extension.';
Which is, by the way, just a shorthand for this:
someString = someString + ' And this is its extension.';
Now, I just said we can't change the value of primitive types, and String is a primitive, so how come this
someString now holds the original string plus the extension that we just added? Well JS did the hard work for you and discarded of your old string, created a new string that has all the parts that you want, and finally assigned its value to your original variable,
someString. So now, check again the value of
And that will print to your Console:
"This is just a random string. And this is its extension."
Voila, our variable has the entire string -- we've "mutated" it even though strings are immutable.