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

What's the Difference Between Class and Prototypal Inheritance?

810


What is a function constructor?

666


What is double exclamation point?

708


Are Javascript and JScript the same?

725


What is a function in javascript and how does it work?

642


Explain how can you submit a form using JavaScript?

639


What's the date object using javascript?

728


What are different types of inheritence? Which inheritance is followed in javascript.

630


List some unit testing frameworks javascript

666


can wwe trace a java program (class)

2080


What are all the types of Pop up boxes available in JavaScript?

789


Explain why asynchronous code is important in javascript?

718


What is difference between undefined variable and undeclared variable?

675


What are all the looping structures in javascript?

696


What are the basic groups of dataypes in JavaScript?

702