Variables and constants in JavaScript

Variables are dynamic in JavaScript, there are names associated with values and they may be reallocated in the program to values of different types: numbers, strings, arrays, etc.

The name of a variable is a series of letters or digits, which starts with a letter or the underscore sign or the $ sign.
For example:

_x name

The single $ symbol represents a variable. It is used by jQuery as equivalent to the object jQquery because, a name can be substituted for another. Example of $ variable name:

$ = "text";

Names are case-sensitive, the name Xeon is different from xeon.

Declaration with the reserved word var or const

A variable is declared by the use of the var reserved word:

var x;

In the case above it will be necessary that it is assigned before it is used.
It is declared also by assigning a value to an identifier:

x = 24; 

A complete declaration would be rather:

var x = 24;

Inside the body of a function, the keyword var is required to create a variable locale to this function.
It is not required in the global space nor for the arguments of functions.
If a variable does not have an assigned value, its contents is undefined, which one can test by a comparison statement:

var y;
if(y == undefined)
   y = 0; 

Constants are declared with the const reserved word (instead of var) and are assigned at the time of the declaration, then it is of course impossible to modify them later on.

const x = 24;

Three primitives and objects

The language primitives are:

They have reserved words and other keywords has been reserved for the future: byte, float, int, short, etc ...

To know the type of a variable, which is defined by the value assigned to it, use typeof. Example:

document.write (typeof Boolean (true));
var x = "text";
document.write (typeof x);

There are objects préféfinis the same name as primitives, but capitalized:

They are initialized by an argument. example:

var x = new Number(50);
var y = new String("text"); 

If we associate a property of the object corresponding to a primitive, example length to a string , it will be dynamically transformed into object for this statement. This does not change the type of the variable beyond the statement.

var a = "text";
document.write(typeof a);
a = new String("demo");
document.write(typeof a);

Scope is local to a function or global

A variable is regarded as global if it is declared out of a definition of function or structure. It is then visible in the body of functions and in structures of the global space or contained in functions.

A variable defined in a function is visible in this function and the body of any structure contained in this function.

But if it is created without the var keyword, it becomes part of the global space, even if it has not been defined outside the function.

function ()
  var x = 1;  
  y = 2;  

The variable x is local, while y is global.

A variable defined as global in a window, can be used in another one by associating its to the name of the window, for example, x is defined in a window whose name is win2:


Values are predefined for conditional tests

Some values are built-in the language:

A boolean value.
The opposite boolean value.
As you may see, the variable is not assigned.
Not A Number. Value to indicate the variable does not hold a number. This value may be assigned, ex: x = NaN, and is tested with the function isNaN().
Has no assigned value.

See also

© 2007-2012