JavaScript - What is the Purpose of Self Executing Function? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report The self-executing anonymous function is a special function which is invoked right after it is defined. There is no need to call this function anywhere in the script. This type of function has no name and hence it is called an anonymous function. The function has a trailing set of parentheses. The parameters for this function could be passed in the parenthesis. Syntax(function (parameters) { // Function body})(arguments); JavaScript (function () { date = new Date().toString(); console.log(date); })(); OutputMon Nov 25 2024 17:19:36 GMT+0000 (Coordinated Universal Time) Why use an anonymous function? The advantage of using an anonymous function instead of writing code directly is that the variables and functions defined within the anonymous function are scoped locally and are not accessible outside of it. This helps prevent cluttering the global namespace with variables and functions that are not needed beyond the function's scope. Anonymous functions can also be used to control access, allowing specific variables and functions to remain private while enabling access to others through closures.Accessing a variable from outside the anonymous functionThis example shows that accessing the date object from outside the anonymous function results in an error. JavaScript (function () { let date = new Date().toString(); console.log(date); })(); console.log('The date accessed is: ' + date); Output Allowing access to one variable to outside the functionThis example shows that the date variable could be made available outside the function by making it global. JavaScript (function () { let date = new Date().toString(); console.log(date); // Assign to global window making it // accessible to outside window.date = date; })(); console.log('The date accessed is: ' + date); Output Comment More info S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like