

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do JavaScript closures work?
In JavaScript, closure is the grouping of a function and where that function declared. In JavaScript, all functions work like closures. A closure is a function uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.
Example
You can try to run the following code to learn how to work with JavaScript closures:
<!DOCTYPE html> <html> <body> <h2>Working with JavaScript Closures</h2> <script> var p = 10; function a() { var p = 15; b(function() { alert(p); }); } function b(f){ var p = 30; f(); } a(); </script> </body> </html>
- Related Questions & Answers
- How do closures work in JavaScript?
- JavaScript closures vs. anonymous functions
- What are JavaScript Function Closures?
- What are Closures in JavaScript?
- Practical Uses for Closures in Javascript?
- How do wind turbines work?
- How do Python modules work?
- How do bitcoin ATMs work?
- How do AC locomotives work?
- Python Closures?
- Implement Private properties using closures in JavaScript
- Using closures to achieve privacy in JavaScript
- How do exceptions work in C++
- How do interfaces work in C#?
- How do arrays work in C#?
Advertisements