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

Why do we need javascript?

470


What is scope variable in javascript?

506


What is a fixed-width table and its advantages?

492


What is the difference between null & undefined?

470


What is the use of document object?

511






What are the problems associated with using javascript, and are there javascript techniques that you discourage?

600


How Does Function Hoisting Work in JavaScript?

519


How can javascript make a web site easier to use? That is, are there certain javascript techniques that make it easier for people to use a web site?

610


What is createtextnode?

480


Which built-in method returns the length of the string?

531


What do mean by NULL in Javascript?

519


How does typeof operator work?

584


What is npm repository?

467


How to set the cursor to wait?

492


What is javascript ‘strict mode’?

488