JavaScript Scope

In JavaScript, scope refers to the visibility of a variable or how it can be used after it is declared. The scope of a variable depends on the keyword that was used to declare it.

The three types of Scope are Global Scope, Function Scope, and Block Scope. Before ES6 (2015), JavaScript had only Global Scope and Function Scope with the var keyword. ES6 introduced let and const which allow Block Scope in JavaScript.

Global Scope: Variables declared outside any function or curly braces ’{}’ have Global Scope, and can be accessed from anywhere within the same Javascript code. var, let and const all provide this Scope.

Function Scope: Variables declared within a function can only be used within that same function. Outside that function, they are undefined. var, let and const all provide this Scope.

Block Scope: A block is any part of JavaScript code bounded by ’{}‘. Variables declared within a block can not be accessed outside that block. This Scope is only provided by the let and const keywords. If you declare a variable within a block using the var keyword, it will NOT have Block Scope.

Block Scope

Before ES6 (2015), JavaScript 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:

Example

{ let x = 2; } // x can NOT be used here

Variables declared with the var keyword can NOT have block scope.

Variables declared inside a { } block can be accessed from outside the block.

Example

{ var x = 2; } // x CAN be used here

Local Scope

Variables declared within a JavaScript function, become LOCAL to the function.

Example

// code here can NOT use carName function myFunction() { let carName = "Volvo"; // code here CAN use carName } // code here can NOT use carName

Local variables have Function Scope:

They can only be accessed from within the function.

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Local variables are created when a function starts, and deleted when the function is completed.

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:

function myFunction() { var carName = "Volvo"; // Function Scope }function myFunction() { let carName = "Volvo"; // Function Scope }function myFunction() { const carName = "Volvo"; // Function Scope }


Global JavaScript Variables

A variable declared outside a function, becomes GLOBAL.

Example

let carName = "Volvo"; // code here can use carName function myFunction() { // code here can also use carName }

A global variable has Global Scope:

All scripts and functions on a web page can access it.


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 var, let and const are quite similar when declared outside a block.

They all have Global Scope:

var x = 2; // Global scopelet x = 2; // Global scopeconst x = 2; // Global scope


JavaScript Variables

In JavaScript, objects and functions are also variables.

Last updated