
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is JavaScript closure? Explain with examples.
Closures in JavaScript allow us to access outer function scope from inner function even after the outer function has executed and returned. This means that the inner function will always have access to the outer function variable.
Following is the code for closures in JavaScript −
Example
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Closures</h1> <div style="color: green;" class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to add two numbers using closure </h3> <script> let sampleEle = document.querySelector(".sample"); let resEle = document.querySelector(".result"); function add(num) { return function add1(num1) { resEle.innerHTML += `${num}+${num1} = ${num + num1}`; }; } document.querySelector(".Btn").addEventListener("click", () => { let storeadd = add(99); storeadd(44); }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- What is context free grammar? Explain with examples
- What is Queue in Python? Explain with examples
- What is 1NF in DBMS? Explain with examples
- Explain JavaScript Regular Expression modifiers with examples
- Explain about EventHandler with Examples in JavaScript
- What is an object in Python? Explain with examples
- What is meant by atomicity? Explain with two examples.
- Explain the finally Statement in JavaScript with examples.
- Explain Closure property of addition and subtraction with example.
- What are Synthetic Fibres? Explain with examples.
- What are natural fibres? Explain with examples.
- Explain Closure property under addition.
- Explain try and catch statements in JavaScript with examples.
- Explain the Error Name Values in JavaScript with examples.
- How to create constants in JavaScript? Explain with examples.

Advertisements