Closures in JavaScript

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will understand closures in JavaScript with an example.

What is a closure
A closure is an inner function that has access to the outer function’s variables in addition to it's own variables and global variables. The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. You create a closure by adding a function inside another function.

JavaScript Closure Example

function addNumbers(firstNumber, secondNumber)
{
var returnValue = "Result is : ";
// This inner function has access to the outer function's variables & parameters
function add()
{
return returnValue + (firstNumber + secondNumber);
}
return add();
}

var result = addNumbers(10, 20);

Output : Result is : 30

The following code Returns the inner function expression

function addNumbers(firstNumber, secondNumber)
{
var returnValue = "Result is : ";
function add()
{
return returnValue + (firstNumber + secondNumber);
}
// We removed the parentheses. This will return the inner function expression without executing it.
return add;
}

// addFunc will contain add() function (inner function) expression.
var addFunc = addNumbers(10, 20);
// call the addFunc() function and store the return value in result variable
var result = addFunc();

Returning and executing the inner function

function addNumbers(firstNumber, secondNumber)
{
var returnValue = "Result is : ";
function add()
{
return returnValue + (firstNumber + secondNumber);
}
// We removed the parentheses. This will return the inner function add() expression without executing it.
return add;
}

// This returns add() function (inner function) definition and executes it. Notice the additonal parentheses.
var result = addNumbers(10, 20)();

Рекомендации по теме
Комментарии
Автор

This was very well explained. I've seen other people over-complicating the topic while explaining. Short and sweet. Thanks a lot.

asifkamranmalick
Автор

You are a vey good teacher.you explain concepts very clear.thank u so much

isunas
Автор

sir! no words. simply super explanation. Great sir. Thank you so much sir.

someshnukala
Автор

Beautifully explained. Great..!! Thanks a lot. Keep

gudapruthvihemanth
Автор

super tutorial on closures! many thanks!

Knowideer
Автор

Thank you so much! I finally understand closures!

abdulshabbir
Автор

Nice way of teaching bro! thanks for doing it, keep doing it.

Saravana.Kumar.
Автор

respected venkat sir, we need reporting like crystal report + jquery .plz do as soon.thanx in advanced

shahnawazatharkazmi
Автор

Sir could we expect Reports in Asp.net.it may be crystal report. Thanks for effort is ur excellency 👍

muhammadzarshid
Автор

Thank you for such a nice explanation. I have a question, what happens if we remove return add from the addNumbers function without changing any code?

priyankashah
Автор

please make few video on "JavaScript Scoping and Hoisting"
I really need that videos

shashwatgupta
Автор

Somehow this didn't work for me. May be this is mozilla firefox issues, so I made a little change.
function y(a, b){
var ResultIs = "Result is ";
return(function xek()
{
return ResultIs + (a+b);
});
}
var x = y(5, 6)();
document.write(x);
And it had started to work.

hookbanner
Автор

can anybody tell me when i need to use closures...? and give me real world example where closure exist..?

hk_build