JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - Variable Scope

Understanding variable scope in JavaScript is fundamental to writing clear and effective code. Scope determines where variables can be accessed or referenced in your code.

There are three main types of scope:

  • block scope
  • function scope
  • global scope

Understanding these scopes and how they differ is crucial for writing effective and error-free JavaScript code.

Understanding var, let, and const

var: The var keyword is the traditional way to declare variables in JavaScript. Variables declared with var have function scope, which means they are accessible anywhere within the function they are declared in. If declared outside of any function, they are globally scoped. One important characteristic of var is variable hoisting, where the declaration is moved to the top of its scope during code execution.

let: The let keyword, introduced in ES6 (ECMAScript 2015), provides block scope for variables. This means variables declared with let are only accessible within the nearest set of curly braces {} (a block) in which they are defined. let helps avoid issues with variable hoisting and provides a clearer, more predictable scope.

const: The const keyword, also introduced in ES6, is similar to let in that it provides block scope. However, const is used to declare variables that cannot be reassigned after their initial assignment. This makes const ideal for values that should remain constant throughout the code.

Block Scope

let and const have block scope, meaning they are only accessible within the nearest set of curly braces {} (a block) in which they are defined.

Example: Demonstrating Block Scope

Javascript
Javascript

. . . .
  • let blockScopedVar = "I am block scoped."; declares a variable within an if block.
  • Inside the block, console.log(blockScopedVar); accesses and prints the variable's value.
  • Attempting to access blockScopedVar outside its block (commented out line) would result in a ReferenceError because blockScopedVar is not accessible outside the block in which it's defined.

Function Scope

var is scoped to the nearest function block, whereas let and const are scoped to the nearest enclosing block.

Example: let and const in Function Scope

Javascript
Javascript

. . . .
  • Inside test_func, var functionScopedVar = "I am function scoped with var"; is accessible anywhere in the function.
  • let blockScopedLet = "I am block scoped with let"; and const blockScopedConst = "I am block scoped with const"; are not accessible outside the if block, demonstrating their block scope.

Global Scope

Variables declared outside any function or block are in the global scope and can be accessed from anywhere in the code.

Example: Variables in Global Scope

Javascript
Javascript

. . . .
  • globalVar, globalLet, and globalConst are declared outside any function or block, placing them in the global scope.
  • These variables are accessible both inside and outside accessGlobalScope, demonstrating global scope accessibility.

Comparison of Scopes

ScopeDeclarationAccessibility
Block Scopelet, constWithin the enclosing {} only.
Function Scopevar, let, constWithin the function it is declared in.
Global Scopevar, let, constAnywhere in the code.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next