Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Closures in JavaScript
Play lesson

JavaScript Tutorial - Closures in JavaScript

4.0 (0)
13 learners

What you'll learn

This course includes

  • 12.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Link for all dot net and sql server video tutorial playlists http://www.youtube.com/user/kudvenkat/playlists Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2014/12/closures-in-javascript.html 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. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 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); document.write(result); 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(); document.write(result); 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)(); document.write(result);

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere