The function in your second example is no longer anonymous... it has a name, initSomething.

The first syntax is commonly used to set up a closure... trapping var x, y, z and what not within it so that they don't conflict with any other variables with the same name outside of the closure.

Answer from Brad on Stack Overflow
🌐
StaticMania
staticmania.com › blog › understanding-javascript-functions
JavaScript Functions: A Beginner's Guide to Syntax & Best Practices - StaticMania
November 24, 2024 - To call a function, you have to write the function with a parenthesis (). Accessing a function without () returns a string version of the entire function's definition, not the result.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Functions
Functions - JavaScript | MDN
With default parameters, a manual check in the function body is no longer necessary. You can put 1 as the default value for b in the function head: ... For more details, see default parameters in the reference. The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
People also ask

What is a function in JavaScript?
A function in JavaScript is a reusable block of code that performs a specific task. Instead of repeating the same code multiple times, you can define a function once and call it whenever needed. Functions improve code organization, readability, and efficiency.
🌐
staticmania.com
staticmania.com › blog › understanding-javascript-functions
JavaScript Functions: A Beginner's Guide to Syntax & Best Practices ...
What is a higher-order function in JavaScript?
A higher-order function is a function that takes another function as an argument or returns a function. These functions are commonly used in functional programming and are found in built-in JavaScript methods like map, filter, and reduce.
🌐
staticmania.com
staticmania.com › blog › understanding-javascript-functions
JavaScript Functions: A Beginner's Guide to Syntax & Best Practices ...
What are the different ways to declare a function in JavaScript?
JavaScript provides several ways to declare functions, including function declarations, function expressions, arrow functions, and immediately invoked function expressions (IIFE). Each method has its own syntax and use cases.
🌐
staticmania.com
staticmania.com › blog › understanding-javascript-functions
JavaScript Functions: A Beginner's Guide to Syntax & Best Practices ...
🌐
W3Schools
w3schools.com › JS › › js_functions.asp
JavaScript Function Study Path
Arrow Functions is a short syntax for function expressions · You can skip the function keyword · You can skip the return keyword · You can skip the curly brackets · Step 8Intermediate · Test your knowledge of JavaScript functions · The quiz uses the examples you learned in the tutorial.
Top answer
1 of 6
4

The function in your second example is no longer anonymous... it has a name, initSomething.

The first syntax is commonly used to set up a closure... trapping var x, y, z and what not within it so that they don't conflict with any other variables with the same name outside of the closure.

2 of 6
3

In JavaScript, functions create new scope. Using a function wrapper around the entire contents of your JavaScript will ensure you never pollute the global scope.

For instance, if you have an HTML file with some JavaScript at the bottom:

<script>
var test = 'hello';
alert(test);          //'hello'
alert(window.test);   //'hello'
</script>

As you can see, the test variable actually becomes a property of the window object (window.test), which is essentially JavaScript's global scope. There are many reasons you don't want to set variables on window, particularly future compatibility problems (what if a later version of ECMAScript defines a test property for window?). Also, using global variables all the time is slow, because the interpreter will need to trek all the way up the scope chain whenever you use test.

The following is functionally identical to the above, without polluting the global scope. It declares an anonymous function using function(), then invokes it immediately with no arguments using (). This is usually called an immediately-invoked function expression or IIFE:

<script>
(function() {
    var test = 'hello';
    alert(test);          //'hello'
    alert(window.test);   //undefined
}());
</script>

Note that this is just a normal anonymous function like any anonymous function. The set of parens after the closing curly brace invoke the anonymous function. The parens around the entire thing tell the interpreter that it's looking at a value, or expression, rather than a function declaration. This value is simply the result of the anonymous function when it's run. That makes the anonymous function work like a simple closure which you, the programmer, can effectively ignore.

Also, you can use two different syntaxes for IIFEs:

(function() {}());
(function() {})();

It's unlikely you'll have problems using either one, but there are some differences that crop up when you've got some syntax problems in your code. IMO you're better off sticking with the first, which is also more clear to read.

--

As to your second question: are the following two equivalent?

(function(){})();

and

function initSomething() {}
initSomething();

Errm, wellll, sort of. You could probably get away with treating them the same, because for most purposes they work the same. That is, in your program you will get the same results with either one (in both cases you're defining a function, then calling it).

But it's important to note the difference between an anonymous function and a function declaration. You can think of anonymous functions as executables, or blocks of code that you can pass around to work as glue when you don't want to define a real, named function. Because they're anonymous, they don't exist in the scope chain, and you can't, for instance, add properties to an anonymous function object and use them later—unless you assign it to a variable first, in which case it's no longer anonymous!

Declaring a function is totally different. It creates a constructor that you can use again and again to create new objects (using new) that can inherit the original function's properties. This is useful for many things, particularly when using frameworks like AngularJS.

function Friend(likes_you) {
    //private property, only accessible to instances of this object
    this.likes_you = likes_you;
}

//add a function as a property of Friend's prototype -
//instances of the Friend constructor can call this function
Friend.prototype.greet = function(greeting) {
    if (this.likes_you) {
        alert(greeting);
    } else {
        alert("I don't like you");
    }
};

var you = new Friend(true);
you.greet('hello!');          //alerts 'hello!'

var guy = new Friend(false);  //can make as any Friend objects as we want
guy.greet('hello!');          //alerts "I don't like you"

Of course, you don't need to do anything like this, but it's good to know what JavaScript is really doing. And that's just the start of the JS rabbit hole ...

🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_builtin_functions.htm
JavaScript Built-in Functions Reference
Here, you can find all the JavaScript's built-in methods on the following classes: The Number object contains only the default methods that are part of every object's definition.
Find elsewhere
🌐
TOOLSQA
toolsqa.com › javascript › functions-in-javascript
What is Functions in JavaScript and How to Define & Call Functions?
June 17, 2021 - We can call a function by using the function name separated by the value of parameters enclosed between parenthesis and a semicolon at the end. Below syntax shows how to call functions in JavaScript:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Functions › Arrow_functions
Arrow function expressions - JavaScript | MDN
February 21, 2026 - const func = () => { foo: 1 }; // Calling func() returns undefined! const func2 = () => { foo: function () {} }; // SyntaxError: function statement requires a name const func3 = () => { foo() {} }; // SyntaxError: Unexpected token '{' This is because JavaScript only sees the arrow function as having an expression body if the token following the arrow is not a left brace, so the code inside braces ({}) is parsed as a sequence of statements, where foo is a label, not a key in an object literal.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › functions-in-javascript
Functions in JavaScript - GeeksforGeeks
JavaScript · const add = function(a, b) { return a + b; }; console.log(add(2, 3)); A new way to write functions using the => syntax. They are shorter and do not have their own this binding, which makes them useful in some cases. JavaScript · const square = n => n * n; console.log(square(4)); IIFE functions are executed immediately after their definition.
Published   January 22, 2026
🌐
Mimo
mimo.org › glossary › javascript › function
Learn about Function in JavaScript to group related code
Then, curly braces ({}) mark out the beginning and end of the function's body. ... Become a full-stack developer. Learn HTML, CSS, JavaScript, and React as well as NodeJS, Express, and SQL
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Function
Function - JavaScript | MDN
Represents the arguments passed to this function. For strict, arrow, async, and generator functions, accessing the arguments property throws a TypeError.
🌐
NewDev
newdev.io › blog › javascript-functions-for-beginners
What are JavaScript Functions? Explained For Beginners - NewDev Blog
September 1, 2022 - In our new function, we added a condition that checks if the value parameter is a number. So, if the value is not a number, the first return statement would terminate the function and return the string, "Value must be a number". The above method we used to define our JavaScript function is called function declaration.
🌐
Dmitri Pavlutin
dmitripavlutin.com › 6-ways-to-declare-javascript-functions
JavaScript Function Declaration: The 6 Ways
March 19, 2023 - Its syntax is similar to function expression, function declaration, or method declaration, just that it requires a star character *. The generator function can be declared in the following forms: ... Open the demo.
🌐
W3Schools
w3schools.com › js › js_function_intro.asp
JavaScript Functions
It is not common to end a function definition with a semicolon. Semicolons are used to separate executable JavaScript statements.
🌐
Medium
medium.com › geekculture › javascript-functions-for-dummies-part-1-function-declarations-vs-expressions-b11ad65c050f
JavaScript Functions for Dummies: Part 1 — Function Declarations vs Expressions | by James Bond | Geek Culture | Medium
April 21, 2021 - The next two core parts of a JavaScript function are the () parameters and the function body {} . Personally, this is where I always get tripped up and get errors when I’m going too fast, so let’s make sure this is super clear. JavaScript functions don’t always have to accept parameters.
🌐
javascript.com
javascript.com › learn › functions
Create Javascript functions with these free resources today
Want to learn all about JavaScript functions? Learn how to write a function, how to use it, and why you should use them in your JavaScript code today!
🌐
W3Schools
w3schools.com › js › js_function_definition.asp
JavaScript Function Definitions
The typeof operator in JavaScript returns function for functions.
🌐
W3Schools
w3schools.com › js › js_function_expressions.asp
JavaScript Function Expressions
Function expressions offer high flexibility and are widely used in JavaScript for various purposes, and you will see a lot more of function expressions in the following chapters: Arrow Functions The concise arrow function syntax (=>) is a modern way of writing function expressions.
🌐
Quora
quora.com › What-is-a-function-What-are-JavaScript-function-examples
What is a function? What are JavaScript function examples? - Quora
Answer (1 of 2): Hello, A function is a group of code that is reusable,it helps you to organize your program,make your code less polluted and helps you make use of the same code that you want to use again and again. In javascript programs,actually functions are what executes basically and gives...