Clean β’ Professional
In JavaScript, scope and hoisting are two fundamental concepts that determine how and where variables and functions are accessed in your code.
Understanding these helps prevent bugs, avoid unexpected results, and write more predictable, efficient programs.
What is Scope?
Scope defines the visibility and lifetime of variables β in other words, where in your program you can access a variable.
In JavaScript, there are three main types of scope:
When you declare a variable inside a function, it is only accessible within that function β not outside it.
Each function creates its own scope.
Example:
function greet() {
let message = "Hello, world!";
console.log(message); // Accessible here
}
greet();
console.log(message); // Error: message is not defined
var
var has function scope.function example() {
console.log(x); // undefined (due to hoisting)
var x = 10;
console.log(x); // 10
}
example();
let and const
let and const have block scope (not function scope).{} where they are declared.function demo() {
if (true) {
let a = 5;
const b = 10;
console.log(a, b); // 5 10
}
console.log(a); // Error: a is not defined
}
demo();
| Keyword | Scope Type | Hoisted | Reassignment | Best Use |
|---|---|---|---|---|
var | Function Scope | Yes (initialized as undefined) | Yes | Legacy code, avoid if possible |
let | Block Scope | Yes (but not initialized) | Yes | Reassignable variables |
const | Block Scope | Yes (but not initialized) | No | Constants and fixed references |
What is Hoisting?
Hoisting is JavaScriptβs default behavior of moving declarations to the top of their scope (before the code executes).
This applies to:
var)It means you can call a function before itβs defined in your code.
Example: Function Hoisting
sayHello(); // Works! Output: Hello there!
function sayHello() {
console.log("Hello there!");
}
| Type | Example | Hoisted? | Usable Before Definition? |
|---|---|---|---|
| Function Declaration | function greet() {} | Yes | Yes |
| Function Expression | const greet = function() {} | Partially (variable hoisted, not function) | No |
| Arrow Function | const greet = () => {} | Partially (variable hoisted, not function) | No |
Example:
greet(); // Error: Cannot access 'greet' before initialization
const greet = function() {
console.log("Hi!");
};
Hoisting Example (Variable + Function)
console.log(num); // undefined
sayHi(); // Hello!
var num = 10;
function sayHi() {
console.log("Hello!");
}Β