Browse By

Lesson #2 – Variables

Although it is not 100% correct to talk about variables before data types I believe it will help to understand the data types better in the next lesson. So what we call variable is a pointer to the place in memory where the value is stored. Or if you like it is a named placeholder that represents the data that you work with. To create a JavaScript variable is pretty easy. Just place the keyword “var” before the name of your choice and assign a value to it. Something like this : var name = “Chris”; You can give any name to your variable and it can be a combination of letters,numbers, dollar signs and underscores. The only restriction is not to start with a number.  

A good idea is to declare all the variable that you are going to use that at the top of your code and at the top of every function. You can declare multiple variable at once by separating the expressions with commas. Like this:

var dad = "Chris", mum="Maria", son="Alex";

Of course there will be cases where you know that you will make use of a variable but you still don’t know the value to assign. Eg. the value will be something that the user will give us or a calculation that have’t happened yet. Still is a good idea to declare the variable but without a value. Like this var x; Later when you have the value you can assign to it but in the meantime JavaScript gives a default value of undefined to it.

Continue to Lesson #3 – Data Types

Leave a Reply

Your email address will not be published. Required fields are marked *