Member-only story

Scope and Hoisting in ES6 JavaScript

MDN
5 min readFeb 25, 2020

--

Since 2015 Developers implemented a convention called ES6 to better use Javascript code; two ways this has changed is though hoisting and scope.

SCOPE

Scope is what’s available, or the resources one has access to, in any execution of code. When looking at scope, the easiest way to see it in action is through the assignment of variables.

Global, Block, Functional Scope

There are two new ways to assign variables, let and const:

let a = "some"
const b = "variables"
console.log(a,b) //some variables

This is an example of global scope. The variables a and b everywhere in the code are the same value. You can reassign the value of a because it was assigned by let ; the value of b can’t be reassigned because it was assigned by const (short for constant).

a = "all" 
'all'
b = "functions"
Thrown: TypeError: Assignment to constant variable.

Without a const or let these variables are also assigned on a global scope.

If we look at how variable assignment works in functional scope, we get to see the value of const and let. If we were interested in knowing the number of chairs in a room, we could create a function like…

--

--

MDN
MDN

Written by MDN

Composer, Designer, Educator

No responses yet

Write a response