JavaScript

How anonymous functions work?

April 29, 2022

How anonymous functions work?

In this article, we will learn how anonymous functions work, when to use them and why anonymous functions are important in JavaScript

how anonymous functions work thumb

What’s up programmer, how are you? Let’s see today how anonymous functions behave in JavaScript!

The idea of the anonymous function is that you can create a function without previously having a name for it

That is, when declaring a variable you can already insert an anonymous function for it.

Let’s see an example:

let test = function() {
 console.log('testing');
}

test();

That is, we can say that anonymous functions do not depend on names, and are only declared in variables.

And of course: you will always call the anonymous function by the name of the variable, since it doesn’t have a name by itself, as seen before

Anonymous function does not differ from a normal JavaScript function when comparing how they both work

The big thing is that we don’t always need to name a function, so anonymous functions make a lot of sense following this approach.

For example: when you want to execute some function when HTML loads

window.onload = function() {
 // code
}

Note that there is no need to create a name for this action.

Conclusion

In this article we learned how anonymous functions work

Basically we use them when we don’t need to declare a function name

This JavaScript functionality makes it easy for us to simply assign a function to a variable and be able to execute it

Do you want to learn more about JavaScript? Click here!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x