JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)
What is scope in javascript
Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.
Practical Code Example
javascript
// Global Scope
const globalVar = 'Global';
function outer() {
// Function / Lexical Scope
const outerVar = 'Outer';
if (true) {
// Block Scope (let & const)
let blockVar = 'Block';
var functionScopedVar = 'Var inside block';
console.log(globalVar, outerVar, blockVar); // Accessible
}
// console.log(blockVar); // ReferenceError: blockVar is not defined
console.log(functionScopedVar); // 'Var inside block' (var ignores block scope)
}
outer();