What is Self-Executing Function? Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 brackets and the parameters can be passed through the round brackets at the end.Syntax(function (parameters) { // Code block to be executed})(parameters); JavaScript (function () { date = new Date().toString(); console.log(date); })(); OutputMon Nov 25 2024 17:15:50 GMT+0000 (Coordinated Universal Time) Naming anonymous functionsWe can assign a name to a self-executing function with the following syntax. This can be used to call the function in the future.Syntax(function_name = function (parameters) { // Code block to be executed})(parameters); JavaScript (print_name = function (name = "Geek") { console.log("The function is executed by " + name); })(); print_name("GFG"); OutputThe function is executed by Geek The function is executed by GFG Why should we use a self-executing function?One of the advantages that we get from a self-executing function is that the variables inside a self-executing function are not accessible outside of the function. This prevents global space to be filled with an extra variable that is not needed and takes up extra space. We can see in the following example the variable created inside the self-executive function is not accessible outside it and results in an error. JavaScript (print_name = function () { let name = "Geek"; console.log("The function is executed by " + name); })(); console.log(name); Output: This will throw an error as the name variable is not defined in the global space. Comment More infoAdvertise with us Next Article What is Self-Executing Function? M maddler Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript - What is the Purpose of Self Executing Function? 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 p 2 min read What are the Functions of a CPU? A central processing unit, or CPU. The main part of a computer is system processing. Because it carries out commands, performs calculations, and regulates the data flow inside the computer That's why CPU is also known as the brain of the computer. The CPU interprets and executes instructions from th 4 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 PHP create_function() Function The create_function() is an inbuilt function in PHP which is used to create an anonymous (lambda-style) function in the PHP. Syntax: string create_function ( $args, $code ) Parameters: This function accepts two parameters which is describes below: $args: It is a string type function argument. $code: 2 min read Named Function Expression In JavaScript or in any programming language, functions, loops, mathematical operators, and variables are the most widely used tools. This article is about how we can use and what are the real conditions when the Named function Expressions. We will discuss all the required concepts in this article t 3 min read Like