0% found this document useful (0 votes)
3 views

Block Scope

Uploaded by

nonocex510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Block Scope

Uploaded by

nonocex510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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:

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, are 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

You might also like