Java Script Syntax - Variables

Variables

Variables in standard JavaScript have no type attached, and any value can be stored in any variable. Variables are declared with a var statement, multiple variables can be declared at once. An identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). Starting with JavaScript 1.5, ISO 8859-1 or Unicode letters (or \uXXXX Unicode escape sequences) can be used in identifiers. In certain JavaScript implementations, the at sign (@) can be used in an identifier, this is contrary to the specifications and not supported in newer implementations. Variables are lexically scoped and once a variable is declared, it may be accessed anywhere inside the function where it is declared, even before the declaration appears. In effect, variable declarations are hoisted or lifted to the top of the enclosing function, but a variable value will still be undefined until it is initialized. Variables declared outside any function are global. If a variable is declared in a higher scope, it can be accessed by child functions.

Here is an example of variable declarations and global values:

var x = 0; // A global variable, because it is not in any function function f { var z = 'foxes', r = 'birds'; // 2 local variables m = 'fish'; // global because it wasn't declared anywhere before function child { var r = 'monkeys'; // This variable is local and does not affect the "birds" r of the parent function. z = 'penguins'; // The child function is able to access the variables of the parent function, this is called closure. } twenty = 20; // This variable is declared on the next line, but usable anywhere in the function, even before, as here var twenty; child; return x; // We can use x here because it is global } f; alert(z); // This line will raise a ReferenceError exception because the value of z is no longer available

When JavaScript tries to resolve an identifier, it looks in the local function scope. If this identifier is not found, it looks in the outer function that declared the local one, and so on along the scope chain until it reaches the global scope where global variables reside. If it is still not found, JavaScript will raise a ReferenceError exception.

When assigning an identifier, JavaScript does exactly the same process to retrieve this identifier, except that if it is not found in the global scope, it will create the "variable" as a property of the global object. As a consequence, a variable never declared will be global if assigned. Declaring a variable (with the keyword var) in the global code (i.e. outside of any function body), assigning a never declared identifier or adding a property to the global object (usually window) will also create a new global variable.

Note that JavaScript's strict mode forbids the assignment of an undeclared variable, which avoids global namespace pollution.

Read more about this topic:  Java Script Syntax

Famous quotes containing the word variables:

    The variables of quantification, ‘something,’ ‘nothing,’ ‘everything,’ range over our whole ontology, whatever it may be; and we are convicted of a particular ontological presupposition if, and only if, the alleged presuppositum has to be reckoned among the entities over which our variables range in order to render one of our affirmations true.
    Willard Van Orman Quine (b. 1908)

    The variables are surprisingly few.... One can whip or be whipped; one can eat excrement or quaff urine; mouth and private part can be meet in this or that commerce. After which there is the gray of morning and the sour knowledge that things have remained fairly generally the same since man first met goat and woman.
    George Steiner (b. 1929)

    Science is feasible when the variables are few and can be enumerated; when their combinations are distinct and clear. We are tending toward the condition of science and aspiring to do it. The artist works out his own formulas; the interest of science lies in the art of making science.
    Paul Valéry (1871–1945)