Javascript - Variable (var)

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.

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" );
};

Property

Management

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

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;

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

Hoisting

See Javascript - (Variable) Scope (Namespace). 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);

Assignment

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

Array Destructuring

See Array_destructuring

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

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"));

defined

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

Type

See Javascript - Type

Example:

var foo=2;

console.log(typeof 2);

Documentation / Reference





Discover More
Javascript - Immediately invoked function expression (IIFE)

An IIFE (pronounced “iffy”) is a function expression that is immediately invoked in order to create a local scope for the variables They therefore won’t affect the surrounding code outside of it...
Javascript Lexical Environment Scope Chaine
Javascript - (Variable) Scope (Namespace)

Variable scope in Javascript. variable scope is delimited by the function definition, not a the block level A block has the scope of its inner function. As a function is also an object, by generalization,...
Javascript - For Statement

The for iterator statement array iteration The colors Reference/Statements/for...in. array The loop iterate over all enumerable properties of the object itself and those the object inherits...
Javascript - Function Expression (Anonymous function)

A Reference/Operators/functionFunction Expression is just an function in the form of an expression stored generally in a variable that you execute by adding its signature ([param,[, param,[..., param]]])...
Global Namespace Web Console Firefox
Javascript - Global (Object|Namespace|window)

The global namespace is the top space of Javascript that contains the variables. In a browser, it's known as the window. Defining global variables pollutes the common namespace shared by everyone, introducing...
Node - Module (ie NPM Module)

The module notion in Node. A module in Node is the source script (not the bundled library/package). The module loader of Node use the commonJs format. Therefore a commonJS Module will run in Node. A...



Share this page:
Follow us:
Task Runner