Skip to content

JavaScript Anonymous Functions | Basics

  • by

Functions without a name are called Anonymous Functions in JavaScript. We use only the function keyword without the function name.

The below code shows how to define an anonymous function:

function() {
    // Function Body

An anonymous function can also have multiple arguments, but only one expression.

JavaScript Anonymous Functions

A simple example is an anonymous function that is not accessible after its initial creation. Therefore, you often need to assign it to a variable. We assign the anonymous function to the show variable.

<!DOCTYPE html>
   let show = function() {
    console.log('Anonymous function');




JavaScript Anonymous Functions

Let’s see how to pass arguments to the anonymous function.

   var greet = function (platform) {
    console.log("Welcome to", platform);

Output: Welcome to EyeHunts!

Callback function to the setTimeout() method. This executes this anonymous function 2000ms later.

setTimeout(function() {
    console.log('Execute later after 2 second')
}, 2000);

Immediately invoked function execution or created a self-executing function.

(function() {

Arrow functions

ES6 introduced the Arrow function expression that provides a shorthand for declaring anonymous functions:

let show = () => console.log('Anonymous function');


let add = (a, b) => a + b;   

Do comment if you have any doubts or suggestions on this JS function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *