Anonymous function versus Normal function in Javascript for beginners

Abideen Muhammed
2 min readOct 15, 2020

--

credit: scriptverse.academy

If you are new to Javascript and you find it difficult to understand what normal function is and what anonymous function is. In this article, I will be explain the two extensively with examples.

Functions generally allow codes to be reusable instead of typing the same thing every time, we will just call the function. Functions conform with the principle in coding: ‘DRY’. DRY is a term that stands for Don’t Repeat Yourself. The opposite of DRY principle is ‘WET’ meaning We Enjoy Typing which is not good.

A JavaScript function is a block of code designed to perform a particular task. To write a function in Javascript, you will start by writing the keyword ‘function’ followed by the variable name and then (condition for the function is put here) and {} in which codes to be executed goes inside the curly bracket, for instance:

function namedFunction (a,b) {
return a+b
}

The above function is called named function or normal function because it has a name or variable immediately after the keyword ‘function’. As for anonymous function, it has no name after the keyword ‘function.’ Although anonymous function has no name, but it can be stored in a variable and run essentially the same way. For example:

var universe = function (num1, num2){
return num1+num2
}

You will observe that the function has no name coming after it, that is why it is called anonymous.

Also, for a function to be executed, it must be called or invoke. For instance, the above normal function will be called in the following way:

namedFunction(4,8) which gives 12 as answers.

Calling anonymous function

In calling anonymous function, you are to call the variable name since there is no function name.Then you can save it to a different variable if you’d like.

let universe = function (num1, num2){
return num1+num2
}
universe(21,21)

I want to believe you have learnt what Normal and Anonymous Functions are. Another thing is there any benefit for using normal function over anonymous function? Named or normal function provides the benefit of readability. Since you have to named it, you end up having to make it more understandable and accessible to developers of all skill level while anonymous functions provides context scoping for easier development.

You can reach out to me on LinkedIn here , or github here.

--

--

Abideen Muhammed

Data Analyst|Data Scientist|Business Analyst|Software Engineer|Machine Learning Enthusiast