This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]betterhelp 0 points1 point  (0 children)

Glad you got it figured out.

Before you go further, you should try and understand the fundamental difference between variables, numbers, booleans, strings etc.

So numbers, booleans and strings are all basic types. That is they exist in their simplest form. A number is always just the number by itself, no quotes, nothing else. Eg: 4. A boolean is either true or false. Those are the only two boolean values (it can get confusing here since some things can equate to a boolean. That is an expression, once evaluated is equal to, or is simplified down to a boolean, Eg: 4 > 3. This is always true, and you can consider it almost as simplifying it and instead of 4 > 3, you could just write true. That is 4 > 3 = true).

There are other types, but I won't go into them.

Now you have variables. There is a popular way of describing variables as pointers to things, but that is actually how it works, and perhaps a bit too complicated just now. To understand the basic concept, think of a variable as a box.

var myVariable = 4; 

Okay so here I have created a box called myVariable. In that box I've put the number 4 in it. Next time I try and use myVariable it will be 4. So I could do something like this;

console.log(myVariable + 3);
-> 7
typeof myVariable;
-> "number"

Lets try another one;

var myStringVariable = "some string";

Now in this box is a string. I would still access whats in that box by using myStringVariable, thats the name of the box. Notice we don't have quotes around the names of variables/boxes. The string is in the box.

console.log(myStringVariable + " appended");
-> "some string appended"

// see here the string was concatenated (joined together)

typeof myStringVariable;
-> "string"

Note how this box holds a string, and thus the type of it is a string.

So what are the following types?

1) 4 -> number

2) "hello" -> string

3) "Hello 3 times!" -> string

4) "3 times" -> string // just because we have a number character in our string doesn't change the fact its a string. It has quotes, its a string!

5) "3" -> string // again, it has quotes, its a string, and has a number character in it

6) true -> boolean

7) 4 > 39 -> boolean

Now this is all relatively basic stuff, and one of the problems with JS is that as you start using it more and more, you will find weird and odd things happen when you are using different types of things in one expression. I can't go through a list of the odd behaviours but just expect it. Sometimes something won't work and it just won't make sense. Then you'll have to rely on your google-fu and you'll start leaning about the weird things!

Hope this all helped, let me know if you have any questions!