0% found this document useful (0 votes)
6 views6 pages

Closure Java

Closures in JavaScript allow inner functions to access variables from their outer function's scope even after the outer function has finished executing. This feature is essential for maintaining state and encapsulation in JavaScript programming. Examples demonstrate how closures can keep references to variables, enabling private data access and manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Closure Java

Closures in JavaScript allow inner functions to access variables from their outer function's scope even after the outer function has finished executing. This feature is essential for maintaining state and encapsulation in JavaScript programming. Examples demonstrate how closures can keep references to variables, enabling private data access and manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Closure

in J avas c ri pt
Closures are a very important feature in
JavaScript. They're fundamental to how
the language works and are used all the
time, even when you don’t realize it.

Here's a brief
explanation: page 01

@diveintodev_
Definition

A closure is created when a function


accesses a variable that is defined in its
outer scope, even after that outer
function has finished executing.

In other words Inner function can


access outer function variable

page 02

@diveintodev_
EXAMPLE - 1

page 03

@diveintodev_
What’s happening here?

outerFunction returns innerFunction.

counter is a variable in outerFunction's


scope

Even after outerFunction is done


executing, innerFunction keeps a reference
to counter.

Every time myClosure() is called, it updates


and logs the same counter. page 04

@diveintodev_
EXAMPLE - 2

Here, count is private. It can only be


accessed via increment and decrement.
page 05

@diveintodev_
Follow for more

Aditya Gaurav
LinkedIn- https://www.linkedin.com/in/adityagaurav-
Instagram - diveintodev_

like share save

You might also like