Variables are commonly explicitly declared the var statement, as shown below:
The above variable is created, but has the default value of undefined. To be of value, the variable needs to be initialized:
var c = 0;
Variables can also be created by assigning directly to them, which creates a global variable:
c = 0;
The above is equivalent of assigning the variable to the window object:
window.c = 0;
and creating such global variables is a practice that is best avoided.
When naming variables there are some rules that must be obeyed:
Boolean variables can only have 2 possible values, true or false.
var mayday = false;
var birthday = true;
You can use Integer and Float types on your variables, but they are treated as a numeric type.
var sal = 20;
var pal = 12.1;
The String and char types are all strings, so you can build any string literal that you wished for.
var myName = "Some Name";
var myChar = 'd';
A complex type is an object, be it either standard or custom made. Its home is the heap and goes everywhere by reference.
var myArray = new Array(0, 2, 4);
var myOtherArray = new Array();
Arrays can also be created with the array notation, which uses square brackets:
var myArray = [0, 2, 4];
var myOtherArray = ;
Arrays are accessed using the square brackets:
myArray = "Hello";
var text = myArray;
It is possible to have thousands of items in an array.
var myObject = new Object();
Iana Tsolova is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.