Anonymous 
Functions 
What is an anonymous function?!
Normal function 
function foo() { 
return "Hello World!"; 
}
Anonymous function 
function() { 
} 
-- A function without a name is called an anonymous 
function! 
BUT the above code won’t work... yet
Functions in JS 
Functions in JavaScript are first-class objects. It means 
that, any functions in JS can be treated like an object! 
So… what does it actually mean? What is the advantage?
function sayHello() { 
return "Hello World"; 
} 
sayHello(); 
The advantage 
can be called as 
(sayHello)();
function sayHello() { 
return "Hello World"; 
} 
(sayHello)(); 
The advantage
function sayHello() { 
return "Hello World"; 
} 
(sayHello)(); 
The advantage 
Let’s take this 
whole code
function sayHello() { 
return "Hello World"; 
} 
(sayHello)(); 
The advantage 
Let’s take this 
whole code 
and paste it 
inside there
(function() { 
return "Hello World"; 
})(); 
The refactored code
(function() { 
return "Hello World"; 
})(); 
Now, it’s an anonymous function! The double parenthesis 
at the end of the function declaration will immediately call 
the function just as you would do with: 
(sayHello)(); 
The refactored code
End 
That’s it!
By 
Mohammed Sazid-Al-Rashid 
- Automation Solutionz 
- @sazidz 
- +SazidAlRashid

Anonymous functions in JavaScript