JavaScriptEasy
Explain the concept of scope in JavaScript
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
In JavaScript, scope determines the accessibility of variables and functions at different parts of the code. There are three main types of scope: global scope, function scope, and block scope. Global scope means the variable is accessible everywhere in the code. Function scope means the variable is accessible only within the function it is declared. Block scope, introduced with ES6, means the variable is accessible only within the block (e.g., within curly braces {}) it is declared.
js
var globalVar = "I am a global var";
function myFunction() {
var functionVar = "I am a function-scoped var";
if (true) {
let blockVar = "I am a block-scoped var";
console.log("Inside block:");
console.log(globalVar); // Accessible
console.log(functionVar); // Accessible
console.log(blockVar); // Accessible
}
console.log("Inside function:");
console.log(globalVar); // Accessible
console.log(functionVar); // Accessible
// console.log(blockVar); // Uncaught ReferenceError
}
myFunction();
console.log("In global scope:");
console.log(globalVar); // Accessible
// console.log(functionVar); // Uncaught ReferenceError
// console.log(blockVar); // Uncaught ReferenceError
