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 infoAdvertise with us Next Article JavaScript - What is the Purpose of Self Executing Function? S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads What is Self-Executing Function? A self-executing function is a function in JavaScript that doesn't need to be called for its execution it executes itself as soon as it is created in the JavaScript file. This function does not have a name and is also called an anonymous function. This function is initialized inside a set of round b 2 min read What is the (function() { } )() construct in JavaScript? If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit 3 min read What is the syntax for leading bang! in JavaScript function ? Before we get to know the syntax for the leading bang! in a JavaScript function, let's see what functions in JavaScript are actually are. JavaScript functions are a set of statements(procedures) that perform some tasks or calculate some value. A function may take some input and return the result to 2 min read JavaScript Nested functions A nested function (also known as an inner function) is a function that is declared within another function (known as the outer function). The inner function has access to the variables of its outer function, forming a lexical scope chain.JavaScriptfunction outer() { console.log('This is the outer fu 5 min read JavaScript Function.prototype.call() Method The call() method allows function calls belonging to one object to be assigned and it is called for a different object. It provides a new value of this to the function. The call() method allows you to write a method once and allows it for inheritance in another object, without rewriting the method f 3 min read Like