This article is taken from JavaScript @ Wikibooks.
JavaScript is a loosely typed language, this means that you don’t have to worry about setting the type of variable and you can change at will in runtime. But when you code, you have to make sure that you are getting the right type at the right time.
Variable declaration
Variables are commonly explicitly declared the var statement, as shown below:
var c;
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.
Naming variables
When naming variables there are some rules that must be obeyed:
Primitive Types
These are treated by Javascript as value types and when you pass them around they go as values. Some types, such as string, allow method calls.
Boolean Type
Boolean variables can only have 2 possible values, true or false.
var mayday = false;
var birthday = true;
Numeric Types
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;
In ECMA Javascript your number literals can go from 0 to -+1.79769e+308. And because 5e-324 is the smallest infinitesimal you can get, anything smaller is rounded to 0.
String Types
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';
Complex Types
A complex type is an object, be it either standard or custom made. Its home is the heap and goes everywhere by reference.
Array Type
Main article: JavaScript/Arrays
In Javascript, all Arrays are untyped, so you can put everything you want in a Array and worry about that later. Arrays are objects, they have methods and properties you can invoke at will. (The ".length" property indicates how many things are currently in the array. If you add more things to the array, the value of the ".length" gets larger). You can build yourself an array by using the statement new followed by Array, as shown below:
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[2] = "Hello";
var text = myArray[2];
It is possible to have thousands of items in an array.
Object Types
An object within Javascript is created using the new operator:
var myObject = new Object();
JavaScript Objects can be built using inheritance and overriding, and you can use polymorphism. There are no scope modifiers, with all properties and methods having public access. More information on creating objects can be found in Object Oriented Programming.
You can access browser built-in objects and objects provided through browser JavaScript extensions.
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.