Difference Between Scope and Closures in JavaScript
Last Updated :
06 Dec, 2023
The scope and closures are fundamental concepts that play a crucial role in how variables are accessed and managed within the functions and blocks of code. In this article, we will learn about the difference between scope and closures in JavaScript.
What is Scope?
The Scope in JavaScript refers to the context or environment in which a variable or function is declared and can be accessed. JavaScript uses function-based scope meaning variables declared inside a function are locally scoped in while variables declared outside any function have global scope.
Example: In this example, a variable named message is declared in the global scope, and its value is both set and accessed within a function, demonstrating the concept of global scope in JavaScript.
JavaScript
let message;
// Declare message in global scope
function GFG() {
message = "Hello, world!";
console.log(message);
}
GFG();
// Now it's accessible globally
console.log(message);
Output:
Hello, world!
Hello, world!
What are Closures?
The Closures occur in JavaScript when a function "remembers" its lexical scope even if it's executed the outside that scope. In other words, a closure allows a function to access variables from its containing function and even after the containing function has finished executing.
Example: In this example, a closure is created where the inner function retains access to the "name" variable from its containing "outers" function, allowing it to display a personalized greeting when invoked.
JavaScript
function outers() {
let name = "Kumar";
function inner() {
console.log("Hello, " + name + "!");
}
return inner;
}
let greeting = outers();
greeting();
Output:
Hello, Kumar!
Difference between scope and closures in JavaScript:
|
Describes the context of the variable access.
| Refers to ability of function to remember and access its lexical scope.
|
The Variables in a scope have limited lifetime.
| The Variables accessed via closures can persist beyond the lifetime of containing function.
|
The Variables within a scope are accessible within that scope.
| The Closures allow access to variables from their containing function even after that function has exited.
|
Helps prevent variable naming conflicts and provides structured access to variables within the function or block.
| Useful for the creating private data maintaining state and implementing advanced patterns like currying and memoization.
|
Similar Reads
Difference between var and let in JavaScript In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable o
3 min read
Difference between var, let and const keywords in JavaScript JavaScript provides three ways to declare variables: var, let, and const, but they differ in scope, hoisting behaviour, and re-assignment rules. Understanding these differences helps write more predictable and maintainable code.var: Declares variables with function or global scope and allows re-decl
6 min read
What is the difference between call and apply in JavaScript ? JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr
2 min read
Difference between forEach() and map() loop in JavaScript The forEach() and map() methods in JavaScript are used to iterate over arrays, but they serve different purposes. forEach() executes a provided function once for each array element without returning a new array, while map() transforms elements and returns a new array.JavaScript forEach() JavaScript'
4 min read
Difference between Methods and Functions in JavaScript Grasping the difference between methods and functions in JavaScript is essential for developers at all levels. While both are fundamental to writing effective code, they serve different purposes and are used in various contexts. This article breaks down the key distinctions between methods and funct
3 min read