Curriculum
Course: React
Login

Curriculum

React

Text lesson

ES6 Variables

Variables

Before ES6, variables were defined with var, which could be assigned to the global object unless strict mode was used, whereas ES6 introduces let and const alongside var for defining variables.

Example

var

var x = 5.6;

Using var outside a function places the variable in the global scope. Inside a function, var is scoped to that function, but when used inside a block (such as a for loop), the variable is still accessible outside of the block.

var has function scope rather than block scope.

Example

let

let x = 5.6;

let is block-scoped and restricted to the block or expression where it is defined, making it available only within that block, such as inside a for loop.

let has block scope.