Let
To create a variable in JavaScript, use the let keyword.
/**
* To create a variable in JavaScript, use the let keyword
*/
let name;
let message = "Hello World";
console.log(name); // undefined
console.log(message); // Hello World
let a = 1, b = 2; // not recommended
Var
Variables, declared with var are visible through blocks.
/**
* In older scripts you find var keyword
* Var has no block scope, not used in modern scripts
*/
{
var a = 1; // global
let b = 2;
}
console.log(a); // 1
console.log(b); // ReferenceError: b is not defined
Const
To declare a constant, we use const instead of let.
/**
* To declare a constant, we use const instead of let
* Constants are named using capital letters and underscores
*/
const COLOR_BLUE = "#00F";
const COLOR_ORANGE = "#FF7F00";
let color = COLOR_ORANGE;
console.log(color); // #FF7F00
Last update: 398 days ago