Menu

1 / 2

What is a Closure?

DEV Community: frontend·SATHISHKUMAR R·about 15 hours ago
#aZqWp5be
#devtofrontend#devto#dev#function#outer
Reading 0:00
15s threshold

A closure is a function that remembers variables from its outer function, even after the outer function has finished executing.

A closure is created when an inner function remembers and accesses variables from its outer scope.

Example:

function outer() {

let name = "Sathish";

function inner() { console.log(name); }

inner();

Enter fullscreen mode

Exit fullscreen mode

}

outer();

Output:

Sathish

Explanation:

outer() creates a variable called name.

inner() accesses the name variable from the outer function.

When inner() runs, it prints "Sathish". This demonstrates closure-related scope behavior.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.