Variable declaration
var,constandletkeywords
Keywords available
- let keyword - scoped variable.
- const keyword - immutable variable.
- var keyword - unlike the others, does not use block-scoping.
See var is bad page for comparisons on var and let or const.
let keyword
Using this approach scopes the variable to a block (such as if, for or function).
A let variable can only be accessed in its current scope and all the inner scopes.
let foo = "A";
if (true) {
// Declared only in inner scope.
let foo = "B";
console.log(foo)
// B
}
console.log(foo) // A
Use let in an if statement. Declare outside the blocks, so the variable persists.
let x
if (true) {
x = 'yes'
} else {
x = 'no'
}
console.log(x)
// yes
Using let is also useful in more complex logic around loops, to force a variable to be declared multiple times.
Here is a something that still happens even with let. The x reference is used as pointer and changing the value of the x object changes all the items in an array pointing to x.
let x = {}
let y = [1, 2, 3]
let z = y.map(i => x)
// [{}, {}, {}]
x.foo = 'bar'
// [{foo: 'bar'}, {foo: 'bar'}, {foo: 'bar'}
const keyword
A const variable cannot be updated or re-declared.
const x = 1
x = 2
// Uncaught TypeError: Assignment to constant variable.
Note it does not freeze a data structure like an array or map. It just prevents reassignment.
const y = []
y = [ 123 ]
// Uncaught TypeError: Assignment to constant variable.
y.push(1)
y
// [ 1 ]
If you want to freeze a data structure so it is truly immutable, use Object.freeze.
const y = [ 1 ]
Object.freeze(y)
y
// [ 1 ]
y.push(2)
// Uncaught TypeError: Cannot add property 1, object is not extensible
// at Array.push (<anonymous>)
It is also block-scoped. So if you declare const variable inside an if statement, you canโt use it outside it.
Scope
Here, the variable exists only in the block and not available outside of it.
if (true) {
let x = 1
console.log(x)
}
console.log(x)
1
ReferenceError: x is not defined
You can use block scope without using an if statement or for loop.
{
let x = 1
console.log(x)
}
console.log(x)
1
ReferenceError: x is not defined
var keyword
Declare
Initialize as undefined.
var x;
x
// undefined
Declare and set
var message = "Hello, World!";
That is the equivalent of:
var message;
message = "Hello, World!";
Functions
function greet() {
// Use global variable.
console.log(message)
// Define function-scoped variable.
var message = "B";
console.log(message)
}
// Define and use global variable.
var message = "A";
console.log(message)
// A
// Use function-scoped variable.
greet()
// B
// Use global variable.
console.log(message)
// A