{"id":109,"date":"2020-07-31T18:14:03","date_gmt":"2020-07-31T18:14:03","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=109"},"modified":"2020-09-19T18:04:51","modified_gmt":"2020-09-19T18:04:51","slug":"what-is-iife-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/what-is-iife-in-javascript\/","title":{"rendered":"What is IIFE in JavaScript?"},"content":{"rendered":"\n<p>An IIFE (Immediately Invoked Function Expression) pronounced as <em>iify <\/em>is a JavaScript function that runs as soon as it is defined. In other words, the function gets executed automatically as soon as the page loads. There is no need to call this function anywhere in the script. JavaScript engine directly interprets the function.<\/p>\n\n\n\n<p>The syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>(function () {\n    statements\n})();<\/code><\/pre>\n\n\n\n<p>IIFE is also known as <strong>Self-Executing Anonymous Function<\/strong>. This type of function has no name and hence it is called an anonymous function.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>var greet= (function () {\n    var message= \"Hello there!\"; \n    return message; \n})(); \n\/\/ Immediately creates the output: \n\"Hello there\"<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> The variables and functions defined within the IIFE  anonymous function are not accessible from the code outside it.  Therefore, an Immediately Invoked Function Expression is a good way to protect the scope of your function and the variables within it. For example, if you try to access the variable <em>message<\/em> from outside scope, it will throw &#8220;<strong>Uncaught ReferenceError<\/strong>: <strong>message is not defined&#8221;<\/strong> error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var greet= (function () {\n    var message= \"Hello there!\"; \n    return message; \n})(); \n\n\/\/ Variable message is not accessible from the outside scope\nalert(message) \/\/ throws \"Uncaught ReferenceError: message is not defined\"\n<\/code><\/pre>\n\n\n\n<p>If you found this useful, please leave your thoughts in the comment box below and follow us.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An IIFE (Immediately Invoked Function Expression) pronounced as iify is a JavaScript function that runs as soon as it is defined. In other words, the function gets executed automatically as&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[63,67,61,65,60,62,100,208,64,66],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-iife-es6","tag-iife-example","tag-iife-full-form","tag-iife-in-javascript","tag-iife-in-js","tag-iife-use-cases","tag-js-interview-question","tag-use-of-iife-in-javascript","tag-what-is-iife","tag-why-use-iife","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":12,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/637"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}