Javascript - Variable (var)

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

Everything in JavaScript is an object, and can be stored in a variable.

In javascript, a variable may then contains a function as value. See function expression. You can then also return a function from a function.

JavaScript is case sensitive — myVariable is a different variable to myvariable.

Advertising

3 - Example

In a console, you can retrieve the value by just calling the variable by name:

var user = "nico", goodInJavascript = false;
 
if (goodInJavascript) {
    console.log("User "+user+" is good in Javascript" );
} else {
    console.log("User "+user+" is NOT good in Javascript" );
};

4 - Property

5 - Management

5.1 - Declaration and Scope

JavaScript implicitly “hoists” (moves) the declaration to the top of the enclosing function and leaves the assignment in place. See hoisting

declaration Scope
var function
let block
const block
Advertising

5.1.1 - Function

You use the var keyword to declare a variable that will belong to the current scope (ie the function or global if at the top level)

var x;
var x, y;

5.1.2 - Global

Without the var keyword, the variable is defined globally. See hoisting

5.1.3 - Block

ES6 lets you declare variables to belong to individual blocks (pairs of { .. }), using the let keyword.

Block initialization with an object.

  • The name of the variable must be the same than the object property.

myObject = {
   prop1: 1,
   prop2: 2
};
 
var {prop1, var1, prop2} = myObject;
 
console.log("prop1 value is: "+prop1);
console.log("var1 value is: "+var1);
console.log("prop2 value is: "+prop2);

5.2 - Hoisting

See hoisting. The declaration of a variable is always moved to the top of its scope.

foo = 2
var foo;

is treated as:

var foo;
foo = 2

Because of hoisting, you may see all var declarations placed at the top of their functions,

Example:

function noHoisting(){
    bar = "global variable declaration because without the var keyword in a function";
}
// Run the function to run the variable declaration
noHoisting();
console.log(bar);
 
function hoisting(){
    bar = "local variable declaration because declared below with the var keyword";
    // thanks to variable hoisting the below statement will placed at the top of its scope (ie the actual function)
    // and the variable will not be considered global anymore but local
    var bar; 
    console.log(bar);
}
hoisting();
console.log(bar);

Advertising

5.3 - Reference

JavaScript allows you to refer to variables that were defined outside of the current function. See Javascript - Closure

function shoeBuilder(){
    var name = "running shoe";
    function coloredShoe(color){
        return color+" "+name;
    }
   return coloredShoe;
};
 
console.log("The function 'coloredShoe' is returned and have still access to the outer shoe 'name' variable.\n"+
   "Thanks to closure");
var shoe = shoeBuilder()
console.log(shoe("blue"));
console.log(shoe("red"));

5.4 - defined

if ("undefined"==typeof myVariable) {
    // DoSomething
    console.log("MyVariable is not defined")
}

5.5 - Type

See Javascript - Type

Example:

var foo=2;
 
console.log(typeof 2);

6 - Documentation / Reference

web/javascript/es/variable.txt · Last modified: 2019/05/14 09:09 by gerardnico