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
🌐
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.
🌐
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.
🌐
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 › Statements › function
function - JavaScript | MDN
July 8, 2025 - function a(b) {} function a(b, c) {} console.log(a.length); // 2 let a = 2; // SyntaxError: Identifier 'a' has already been declared
Find elsewhere
🌐
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.
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 ...

🌐
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.
Top answer
1 of 3
236

It's a Generator function.

Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function.


Historical note:

It's a proposed syntax for EcmaScript.next.

Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.

Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.

You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators

The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.

You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.

Overview

First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.

Examples

The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):

function* fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;
    }
}

Generators can be iterated over in loops:

for (n of fibonacci()) {
    // truncate the sequence at 1000
    if (n > 1000)
        break;
    print(n);
}

Generators are iterators:

let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
2 of 3
57

It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...

Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.

🌐
Medium
medium.com › @hustonk2 › javascript-functions-syntax-6dcd6b07d474
JavaScript Functions Syntax. A JavaScript function is a block of… | by Katie Gill | Medium
September 25, 2023 - The above function example does not require any parameters and was simply invoked by calling the function in the JavaScript code. The ( ) parentheses operator is what invokes the function.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-function-expression
JavaScript Function Expression - GeeksforGeeks
JavaScript · const greet = function(name) { return `Hello, ${name}!`; }; console.log(greet("Steven")); The function(name) creates an anonymous function assigned to the greet variable. The function takes name as a parameter and returns a greeting string. Calling greet("Ananya") invokes the function and outputs the greeting. Syntax ·
Published   January 23, 2026
🌐
Codecademy
codecademy.com › learn › introduction-to-javascript › modules › learn-javascript-functions › cheatsheet
Learn JavaScript: Functions Cheatsheet | Codecademy
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity. Beginner Friendly.Beginner Friendly15 hours15 hours · Arrow function expressions were introduced in ES6. These expressions are clean and concise. The syntax for an arrow function expression does not require the function keyword and uses a fat arrow => to separate the parameter(s) from the body.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
The "new Function" syntax
October 22, 2020 - The syntax for creating a function: let func = new Function ([arg1, arg2, ...argN], functionBody); The function is created with the arguments arg1...argN and the given functionBody.
🌐
DEV Community
dev.to › koladev › 5-ways-to-write-functions-in-javascript-17d5
5 Ways to Write Functions in JavaScript - DEV Community
August 9, 2022 - Using var, const, or let keyword, followed by the function name and affectation sign (= in JavaScript), you enter the keyword function followed by parameters in brackets and a pair of curly braces containing ...
🌐
Programiz
programiz.com › javascript › function
JavaScript Function and Function Expressions (with Examples)
Note: Like with functions, we need to use parentheses () with the variable name to call a function expression. ... Before we wrap up, let’s put your knowledge of JavaScript Function and Function Expressions to the test!