In this Javascript Variables Var Keyword tutorial the concept what the ‘var’ keyword in javascript is made clear. In javascript a value is stored in a var name.

In javascript all types of values can be stored using the ‘var’ keyword. Numbers, Strings, Booleans, Arrays, Functions and Objects.

In the example above the value on line 9 the variable is a number. We can check this by applying the ‘typeof‘ operator to each var name and then displaying the result in the browser with the ‘document.write()’ method.

Javascript Variables Var Keyword. A number.
The variable named ‘x’ is a number type.

In the next example on line 9 we will change the value from the Number 0 to the String “Javascript”.
This time the ‘typeof‘ operator is again applied to the variable x and then displayed in the browser once again.

Javascript Variables Var Keyword. A string.
The value “Javascript” with the var name x is a ‘string‘ type.

Another type in javascript is the ‘Boolean‘ type. A Boolean can only be one of two values, (true or false). On line 9 lets change the value passed to the variable ‘x’ from “Javascript” to ‘true‘.

Javascript Variables Var Keyword. A boolean.
The value of ‘true’ passed to the variable ‘x’ is of the type ‘boolean’.

If we put quotes around the word ‘true’ on line 9 the type will change from ‘boolean’ to ‘string’. Strings in javascript are always initialised with quotes around the value.

Javascript Variables Var Keyword. A string.
The value “true” with the var name x is now a ‘string‘ type.

Another type in javascript is the array type. Below on line 9 the variable ‘x’ is now an array.

Javascript Variables Var Keyword. An object.
Surprisingly the typeof method returns object for the ‘[]’ value that is passed to the variable x.

We can still check to see if the value ‘[]’ is indeed an array.

Javascript Variables Var Keyword. A true value.
The value of ‘[]’ passed to the variable ‘x’ is an array as true is returned.

Notice how on line 10 the ‘Array.isArray‘ method
has been used to determine if the value of ‘x’ is an array.

Next on line 9 the curly braces ‘{}’ are now passed to the variable ‘x’.

Javascript Variables Var Keyword. An object.
The typeof method returns object for the ‘{}’ value that is passed to the variable x.

Javascript Variables Var Keyword. a function.
The value on line 9 that is passed to the variable ‘x’ is a javascript ‘function’ type.

This Javascript Variables Var Keyword tutorial is an introduction to the javascript keyword ‘var’. A value can be stored in a var name and any of the six types found in javascript can be stored in a var name.


More on Javascript

Javascript logo.

Share!Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on Tumblr

Leave a Reply

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