What is Anonymous Function?
Anonymous functions in javascript are those functions that are created without any identifier or name to refer to it.
Example:
var myFunc = function() { alert('Anonymous Function'); } myFunc();
Use of Anonymous Function
- Anonymous functions can be used as arguments to other functions.
setTimeout(function() { alert('hackinbits'); }, 1000);
- Anonymous functions are very useful in creating IIFE(Immediately Invoked Function Expression).
(function(text) { alert(text); }('hackinbits'));
3 Anonymous functions are commonly used for creating function expressions.
let myFunc = function(text){ alert('Inside Function expression'); };
What is Named Function?
Named functions are normal functions, with name or identifier to refer to it. For example,
function myFunc() { alert('Named Function'); } myFunc();
Benefits of Named Function
Named function is very useful in javascript. Some of the benefits of using named functions are listed below:
-
Named functions are very helpful in debugging, in knowing which function caused an error, as you will get the function name in the error log.
-
Named functions are more readable, thus helping your code more understandable by other developers.
-
Named Functions are easier to reuse and thus helps you in writing clear code.