Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Variable Declaration Scope in JavaScript

This document explains variable declaration scope in JavaScript, highlighting three types: Global Scope, Function Scope, and Block Scope. Global variables are accessible throughout the code, function-scoped variables are limited to their function, and block-scoped variables (declared with let or const) are restricted to their block. Examples are provided to illustrate each type of scope.

Uploaded by

mdmukimuddin453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Variable Declaration Scope in JavaScript

This document explains variable declaration scope in JavaScript, highlighting three types: Global Scope, Function Scope, and Block Scope. Global variables are accessible throughout the code, function-scoped variables are limited to their function, and block-scoped variables (declared with let or const) are restricted to their block. Examples are provided to illustrate each type of scope.

Uploaded by

mdmukimuddin453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Variable Declaration Scope in JavaScript

Variable scope in JavaScript determines where variables can be accessed or referenced. This
document explores the different types of scope in JavaScript.

Types of Scope
1. Global Scope
2. Function Scope
3. Block Scope

1. Global Scope

Variables declared outside any function or block are in the global scope. These variables are
accessible from anywhere in the code.
<script>
var globalVar = "I am global";
let globalLet = "I am also global";
const globalConst = "I am a global constant";

function checkGlobal() {
console.log(globalVar); // I am global
console.log(globalLet); // I am also global
console.log(globalConst); // I am a global constant
}
checkGlobal();
console.log(globalVar); // I am global
console.log(globalLet); // I am also global
console.log(globalConst); // I am a global constant
</script>
2. Function Scope

Variables declared within a function using `var` are scoped to that function. These variables are
not accessible outside the function.

<script>
function localScope() {
var localVar = "I am local";
console.log(localVar); // I am local
}
localScope();
console.log(localVar); // ReferenceError: localVar is not defined
</script>

3. Block Scope
Variables declared with `let` or `const` within a block `{}` are scoped to that block. This means
they cannot be accessed from outside the block.

<script>
if (true) {
let blockScopedLet = "I am block scoped";
const blockScopedConst = "I am also block scoped";
console.log(blockScopedLet); // I am block scoped
console.log(blockScopedConst); // I am also block scoped
}

console.log(blockScopedLet); // ReferenceError: blockScopedLet is not defined


console.log(blockScopedConst); // ReferenceError: blockScopedConst is not defined
</script>

-Mohammad Afzal Hossain

You might also like