Sitemap
codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Understand Closures in JavaScript

5 min readJul 24, 2017

--

Open and Closure. Photo via unsplash.com

What’s a closure?

Nested Functions

var sayHello = speak();
sayHello;//  function logIt() {
// var words = 'hi';
// console.log(words);
// }
sayHello();
// 'hi'
var sayHello = speak();
sayHello//  function logIt() {
// console.log(words);
// }
sayHello();
// 'hi'

Example #2

var j = name('John');
var c = name('Cindy');
j;//  function (a) {
// return `${n} likes ${a}`;
// }
j('dogs');  // 'John likes dogs'
c('cats'); // 'Cindy likes cats'

Want more advanced JavaScript?

Closing Notes

Check out my recent articles:

If this post was helpful, please click the clap 👏button below a few times to show your support! ⬇⬇

--

--

codeburst
codeburst

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Brandon Morelli
Brandon Morelli

Written by Brandon Morelli

Creator of @codeburstio — Frequently posting web development tutorials & articles. Follow me on Twitter too: @BrandonMorelli

Responses (20)