What is a “closure” in JavaScript? Provide an example

Answer Posted / hrpatelsoft@gmail.com

var globalVar = "xyz";

(function outerFunc(outerArg) {
var outerVar = 'a';

(function innerFunc(innerArg) {
var innerVar = 'b';

console.log(
"outerArg = " + outerArg + "
" +
"innerArg = " + innerArg + "
" +
"outerVar = " + outerVar + "
" +
"innerVar = " + innerVar + "
" +
"globalVar = " + globalVar);

})(456);
})(123);

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which built-in method returns the calling string value converted to upper case?

606


What is the difference between window and document in Javascript?

616


How do you check if a value is a number in javascript?

569


How do I open javascript in browser?

539


What is difference between module.exports and export?

526






Explain javascript accessors ?

571


How to remove duplicates from javascript array?

601


How do I run javascript in chrome?

565


How to open url in a new tab in javascript?

604


What is the use of a map object in javascript?

533


How to encode and decode a url in javascript?

617


What is the use of a date object in javascript?

647


List html dom mouse events?

587


Text from your clipboard?

582


Is webassembly faster than javascript?

564