Scope

Scope

Published by: Anil K. Panta

Scope

Scope determines the accessibility (visibility) of variables.

JavaScript variables have 3 types of scope:

  • Block scope

  • Function scope

  • Global scope


Block Scope

Before ES6 (2015), JavaScript variables had only Global Scope and Function Scope. ES6 introduced two important new JavaScript keywords: let and const. These two keywords provide Block Scope in JavaScript. Variables declared inside a { } block cannot be accessed from outside the block:



Variables declared with the var keyword can NOT have block scope. Variables declared inside a { } block can be accessed from outside the block.



Function Scope

JavaScript has function scope. Each function creates a new scope. Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var, let and const are quite similar when declared inside a function.

They all have Function Scope:


Global Scope

Variables declared Globally (outside any function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program. Variables declared with varlet and const are quite similar when declared outside a block.

They all have Global Scope:


We will get back to you via email or phone call