Tutorial Playlist

JavaScript Tutorial: Learn JavaScript from Scratch


An Introduction to JavaScript: Here Is All You Need to Know

Lesson - 1

JavaScript Hello World: Here's What You Need To Know

Lesson - 2

All You Need to Know About JavaScript Arrays

Lesson - 3

Using Array Filter In JavaScript to Filter Array Elements

Lesson - 4

Everything You Need to Know About Array Reduce JavaScript

Lesson - 5

Introduction To JavaScript Loops: Do-While, For, For-In Loops

Lesson - 6

All You Need to Learn About Javascript Functions

Lesson - 7

The Best Guide on How to Implement JavaScript Closures

Lesson - 8

JavaScript “This” Keyword and How to Implement It

Lesson - 9

How to Implement JavaScript Form Validation

Lesson - 10

An Introduction Guide To JavaScript RegEx

Lesson - 11

How to Validate an Email Address in JavaScript?

Lesson - 12

All You Need to Know About JavaScript Promises

Lesson - 13

How to Implement JavaScript Async/Await

Lesson - 14

JavaScript DOM Tutorial: 2020 Edition

Lesson - 15

JavaScript Objects: Properties, Methods, and Accessors

Lesson - 16

An Introduction to Javascript Games: The Best Guide

Lesson - 17

An Easy Guide To Build A Calculator App In JavaScript

Lesson - 18

Javascript Projects: The Best Guide

Lesson - 19

What is OOP in JavaScript? How is it Implemented?

Lesson - 20

The Best Guide to Understanding JavaScript Learning Path

Lesson - 21

Java Vs .NET: Which Is the Best Technology to Choose?

Lesson - 22

Top 80+ JavaScript Interview Questions and Answers for 2022

Lesson - 23

Typeof in JavaScript: Checking Data Types Using the Typeof Operator

Lesson - 24

Tips and Tricks That You Should Know Before Going to a Coding Interview

Lesson - 25

Callback Functions in JavaScript

Lesson - 26

JavaScript Events

Lesson - 27
The Best Guide on How to Implement JavaScript Closures

Programmers frequently use JavaScript Closures. Many might not be aware of exactly what a closure is, although its results are present. Nevertheless, it’s always better to have practical knowledge of such concepts. 

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

What Is Variable Scope?

The location of the variable declaration generally defines the scope of a variable. It defines the part of the program where a particular variable can be accessed. JavaScript supports two types of variable scopes - 

  1. Global Scope/Variables
  2. Local Scope/Variables

Global Variables 

A variable declared outside of a function is attributed to the global scope and is hence accessible from anywhere in your code. 

Local Variables

Local Variables or Function Variables are variables declared within a function. These variables are only accessible from that function and its nested functions. 

  • Consider this code to understand the working of global and local variables:


            var a = 10

            function function_Name(){

                var b = 20; 

                document.write(“The sum is “ + (a+b) + '<br>')




  • The output of the above code is: 


Variable Scope 

  • In the above example, the variable “a” is a global variable and can be accessed from anywhere in the code. In contrast, variable “bis a local variable and can be accessed within the function “function_Name” and perhaps its nested functions. 
  • The scope chain is a stack consisting of all the references to the variables for the function being executed. 

Now that you know what variable scope let’s understand what exactly is JavaScript Closure

What Is a Closure?

Closures in JavaScript is a feature where an inner function has access to the outer function’s variables. Closures are important as they define what is and what isn’t in the scope of functions. They also control the variables that are being shared with the nested functions. 

A closure –

  1. Has access to the variables of its scope. 
  2. Has access to the variables of the outer functions.
  3. Has access to the global variables.
  • Consider the following piece of code: 


            var a = 10

            function First_func()


                var b = 20;

                function Second_func()


                    var c = 20+a+b

                    return c


                return Second_func();



            var sum = First_func()

            document.write("The sum is " + sum)


  • The variable “a” is a global variable and can be accessed from anywhere within the program. The variable “b” is declared within the function “First_func” and the variable “c” is declared within the function “Second_func” which is a nested function. 
  • Since a closure is a function that has access to the variable from another function’s scope,  function “Second_func” can access all the variables a,b and c. 
  • The output of the code above is shown below: 


JavaScript Closures

  • This was a simple introduction to Closures in JavaScript. We recommend you play around with variables and understand their behavior within different scopes. 
Master JavaScript fundamentals including, jQuery, Ajax, and more with the Javascript Certification Training Course. Check out the course preview!

Moving Foward

To learn more about JavaScript in its entirety, certification is highly recommended and could act as a catalyst for enriching your coding career. Simplilearn's JavaScript Certification Training course helps individuals master the JavaScript programming language in an all-inclusive training program that includes complete JavaScript fundamentals, jQuery, Ajax, and more. You will also have the opportunity to apply your skills by building a real-time chat application.

Do you have any questions regarding JavaScript Closures? Let us know in the comments section of this article. Our experts will get back to you as soon as possible!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.