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 - 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

  • In a browser, without the var keyword, the variable is defined globally. See hoisting
  • In Node, a global variable should be attached to the global namespace
global.myVariable

5.1.3 - Hoisting

See hoisting. Hoisting is a javascript rule applied to variable declaration. Ie the a variable declaration 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.2 - Assignment

5.2.1 - Object Destructuring

ES6 lets you declare variables with values that belongs to 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.2 - Array Destructuring

See Array_destructuring

const fruit = ['banana', 'yellow'];
const [name,color]=fruit;
console.log("This is a "+color+" "+name+".");

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/06/17 12:43 by gerardnico