JavaScript Editor JavaScript Debugger     JavaScript Editor 



Team LiB
Previous Section Next Section

Variables

A variable stores data. Every variable has a name, called its identifier. Variables are declared in JavaScript using var, a keyword that allocates storage space for new data and indicates to the interpreter that a new identifier is in use. Declaring a variable is simple:

var x;

This statement tells the interpreter that a new variable x is about to be used. Variables can be assigned initial values when they are declared:

var x = 2;

In addition, multiple variables can be declared with one var statement if the variables are separated by commas:

var x, y = 2, z;

You should not use variables without first declaring them, although it is possible to do so in certain cases. Using a variable on the right-hand side of an assignment without first declaring it will result in an error.

Experienced programmers will notice that, unlike C, C++, and Java, there is only one way to declare a variable in JavaScript. This highlights the fact that JavaScript’s treatment of variable data types is fundamentally different from many languages, including C, C++, and Java.


Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Debugger     JavaScript Editor


©