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.
Articles Related
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
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
Example:
var foo=2;
console.log(typeof 2);