Difference between Anonymous and Named functions in JavaScript
Last Updated :
27 Jan, 2022
In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to know the named and anonymous functions in and out.
Anonymous Functions: As the name suggests, “anonymous function” refers to a function that does not have a name or a title. In JavaScript, an anonymous function is something that is declared without an identification. It’s the distinction between a regular and an anonymous function. An anonymous function cannot be accessed after it is created; it can only be retrieved by a variable in which it is stored as a function value. There can be several arguments to an anonymous function, but only one expression.
Syntax:
function(){
// Function Body
}
Let’s understand through examples.
Example 1: We build an anonymous function that prints a message to the console in this example. After that, the function is saved in the test variable. We can call the function by invoking test().
Javascript
<script>
var test = function () {
console.log( "This is an anonymous function!" );
};
test();
</script>
|
Output:
This is an anonymous function!
Example 2: The anonymous function can also take arguments.
Javascript
<script>
var test = function (platform) {
console.log( "This is an anonymous " , platform);
};
test( "function!" );
</script>
|
Output:
This is an anonymous function!
We can indeed pass anonymous functions as parameters into another function because JavaScript allows Higher-Order Functions.
Example 3: Here, we send an anonymous function to the setTimeout() method as a callback function. This calls the anonymous function 1500 milliseconds later.
Javascript
<script>
setTimeout( function () {
console.log( "This is an anonymous function!" );
}, 1500);
</script>
|
Output:
This is an anonymous function!
Named Functions: In JavaScript, named functions are simply a way of referring to a function that employs the function keyword followed by a name that can be used as a callback to that function. Normal functions with a name or identifier are known as named functions. They can be employed in an expression or declared in a statement. The function’s name is stored within its body, which can be handy. And we may use the name to get a function to call itself, or to retrieve its attributes like every object.
Syntax:
function displayMessage(){
// function..body
}
Let’s understand the concept with some examples:
Example 1: We will define a normal function that prints a message in this example.
Javascript
<script>
function test() {
console.log (`This is a named function !`);
};
</script>
|
Output:
This is a named function!
Example 2: You can add named functions to variables on objects, and then call the function on the object.
Javascript
<script>
function test() {
console.log (`This is a ${ this .func}`);
}
const func = {
func: 'named function!' ,
test
}
func.test();
</script>
|
Output:
This is a named function!
Example 3: In JavaScript, named functions are one of the most frequent ways to call functions. You’ll run into them all the time. Another point worth mentioning here is how to use the return keyword to return a value from a function. This isn’t limited to named functions.
Javascript
<script>
function createPerson(firstName, lastName) {
return {
firstName: firstName,
lastName: lastName,
getFullName() {
return firstName + ' ' + lastName;
}
}
}
let name = createPerson( 'This is a' , 'named function!' );
console.log(name.getFullName());
</script>
|
Output:
This is a named function!
Difference between Anonymous and Named functions:
Anonymous functions
|
Named Functions
|
In JavaScript, anonymous functions (function expression) are those that are produced without a name or identifier to refer to them; they are defined as :
function(){
// Function Body
}
|
Normal functions (function declaration) having a name or identifier to refer to them are referred to as named functions and are defined as:
function displayMessage(){
// function..body
}
|
Anonymous functions are never hoisted (loaded into memory at compilation). |
Named functions are hoisted (loaded into memory at compilation). |
When invoking an anonymous function, you can only call it after the declaration line. |
A name function can be invoked before declaration. |
Before ES6, the name properties of anonymous functions were set to the empty string. |
Before ES6, the name properties of named functions were set to their function names. |
For easier programming, anonymous functions allow context scoping. When functions are utilized as data, arrow functions should be used. |
Because the function name appears in the error log, named functions are highly useful in debugging and determining which function produced an error. |
Anonymous functions can be very handy when developing IIFEs (Instantly Invoked Function Expressions). |
Named functions are easier to reuse, which aids in the creation of clean code. |
Similar Reads
Difference between Methods and Functions in JavaScript
Grasping the difference between methods and functions in JavaScript is essential for developers at all levels. While both are fundamental to writing effective code, they serve different purposes and are used in various contexts. This article breaks down the key distinctions between methods and funct
3 min read
Difference Between Array.from and Array.of in JavaScript
JavaScript provides various methods for creating and manipulating arrays, two of which are Array.from and Array.of. These methods are part of the ECMAScript 6 (ES6) specification and offer distinct ways to create arrays. Understanding the differences between these two methods is important for effici
3 min read
What is the Difference Between ' and " in JavaScript?
In JavaScript, strings are denoted by both single quotes (') and double quotes ("). Both of them serve the same main purpose: to hold text values in your code. This is where the difference comes in when one type of quote is embedded within another. This article is going to explain how and when to us
4 min read
Difference between First-Class and Higher-Order Functions in JavaScript
Understanding the difference between first-class and higher-order functions in JavaScript is important. These are big concepts in programming, especially in the kind of coding used for making websites. This article is all about explaining what they are, how they are used, and why they are so importa
3 min read
Understanding the Difference between Pure and Impure Functions in JavaScript
In this article, we will see the concepts of pure and impure functions in JavaScript, along with understanding their differences, & basic implementation for a better understanding of the concepts. Pure Functions: This function always returns the same output as given the same input parameters. Pu
5 min read
Difference between var and let in JavaScript
In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable o
3 min read
Difference Between for...in and Object.keys() in JavaScript
The for...in and Object.keys() in JavaScript are used to iterate over the properties of an object. While they might seem similar at first glance they have distinct usage, behavior, and characteristics. This article will explore these differences in detail. These are the following topics that we are
3 min read
Difference between JavaScript and AngularJS
In this article, we will see what are JavaScript & AngularJS and its features along with the understanding of their basic implementation. Finally, will discuss the differences between them. JavaScript is a lightweight and object-oriented scripting language used to create dynamic HTML pages with
4 min read
Difference Between Default & Named Exports in JavaScript
In JavaScript, exports allow you to share code between modules. There are two main types: default exports and named exports. Used to export functions, objects, or variables.Default exports allow importing with any name.Named exports require importing by the exact name.Named ExportsNamed exports let
4 min read
What is the difference between call and apply in JavaScript ?
JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr
2 min read