JavaScript

Difference Between Inline and Anonymous Functions in JavaScript

JavaScript, the inline and anonymous functions are mostly utilized to apply a specific functionality or an event upon a particular function. In addition to that, they are very useful in reducing code complexity and making it readable. Also, these functions avoid namespace pollution and are convenient to access as well.

This article will discuss the inline and anonymous functions and their differences in JavaScript.

Inline and Anonymous Functions and their Differences in JavaScript

The “Inline” and “Anonymous” functions are nearly the same as both are created at runtime. The difference is that the inline functions are stored in a specific variable which is not the case in anonymous functions.

Now, let’s study each of them one by one!

What are Inline Functions in JavaScript?

Inline functions are a type of anonymous function contained in a variable. It is similarly created as the anonymous function and then contained in a specific variable.

The following examples will elaborate on the stated concept using the “setTimeout()” method.

Example: Using Inline Function in JavaScript

Firstly, we will include the heading in the “<h3>” tag and align it to center using the “<center>” tag:

<h3><center>The Inline function is stored in a variable</center></h3>

Next, store the specified function in the variable named “inlineFunc”. In its function definition, alert the following message after the specified time out as “2” seconds:

let inlineFunc = function(){

  alert ('This is Inline Function')

};

setTimeout(inlineFunc, 2000)

It can be observed that the added message in the inline function is displayed in the alert box after two seconds:

Example 2: Using Inline Arrow Function in JavaScript

First, we will add a heading as discussed in the previous example:

<h3><center>The Inline arrow function is stored in a variable</center></h3>

Next, apply the arrow function and similarly store it in the variable named “inlineFunc”. Also, apply the “setTimeout()” method to display the corresponding message after the stated time:

let inlineFunc = () => alert('This is Inline Arrow Function');

setTimeout(inlineFunc, 2000)

Output

What are Anonymous Functions in JavaScript?

The JavaScript anonymous functions are declared without any named identifier, as its name suggests.

Example: Using Anonymous Function in JavaScript

We will include the following heading in the center using the discussed tags in the previous examples:

<h3><center>Anonymous function is defined without any name identifier</center></h3>

After that, apply the “setTimeout()” method to the anonymous function(having no name). Also, alert the following message after the set time which is two seconds:

setTimeout(function(){

alert('This is Anonymous Function')

}, 2000);

Output

Example: Using Anonymous Arrow Function in JavaScript

As discussed in the previous methods, we will add a heading using the <h3> tag and align it in the center:

<h3><center>Anonymous arrow function is defined without any name identifier</center></h3>

Then, apply the “setTimeout()” method to the anonymous arrow function having the specified time out:

setTimeout(() =>alert('This is Anonymous Arrow Function'), 2000);

Output

We have discussed the examples to implement the inline and arrow functions in JavaScript.

Conclusion

In JavaScript, the inline and anonymous functions are different in such a way that the inline functions are a type of anonymous function that is stored in a specific variable, whereas the anonymous function is a function without any name. Both of the functions can be created at runtime. This manual guided about the inline and anonymous functions and their differences in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.