{"id":1169,"date":"2023-05-21T07:00:10","date_gmt":"2023-05-21T07:00:10","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=1169"},"modified":"2023-05-21T07:00:11","modified_gmt":"2023-05-21T07:00:11","slug":"how-closures-work-in-javascript","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/21\/how-closures-work-in-javascript\/","title":{"rendered":"How closures work in JavaScript?"},"content":{"rendered":"\n<p>The closure is a locally declared variable related to a function that stays in memory when it has returned.<\/p>\n\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function greet(message) {\n    console.log(message);\n}\nfunction greeter(name, age) {\n\n    return name + \" says howdy!! He is \" + age + \" years old\";\n}\n\/\/ Generate the message\nvar message = greeter(\"James\", 23);\n\/\/ Pass it explicitly to greet\ngreet(message);\nThis function can be better represented by using closures\nfunction greeter(name, age) {\n    var message = name + \" says howdy!! He is \" + age + \" years old\";\n    return function greet() {\n        console.log(message);\n    };\n}\n\/\/ Generate the closure\nvar JamesGreeter = greeter(\"James\", 23);\n\/\/ Use the closure\nJamesGreeter();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The closure is a locally declared variable related to a function that stays in memory&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1169"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1170,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1169\/revisions\/1170"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}