Functions - Javascript Anonymous Function
Posted by Superadmin on May 05 2023 13:28:27

Javascript Anonymous Function

By Priya PedamkarPriya Pedamkar
  

Javascript Anonymous Function

Introduction to Javascript Anonymous Function

Name Anonymous indicates without a name or unknown nameSo, an anonymous function defined as a function without a name whenever a function wants to return other functions than simply used this anonymous function concept.

Syntax:

function print(a){
return function (b)
{
return a*b;
}; //anonymous function
}

Anonymous functions also used for arguments to other functions.

functionName(function () {
document.write("Hi Amar");
}, arguments);

How does Anonymous Function Work in JavaScript?

Anonymous functions work without a name. Surprised right? We will see with an example.

1. General Function

Code:

function getMyName(){
document.write("I am Paramesh");
}
document.write(getMyName());

Output:

Javascript Anonymous Function eg1

2. Anonymous Function

Code:

name=function (){
document.write("I am Paramesh");
}
document.write(name());

Output:

Javascript Anonymous Function eg1

Explanation:

Javascript Anonymous Function Examples with Code

Here are some of the examples of an anonymous function, which are as follows:

1. Anonymous Function without Parameter

Syntax:

var variableName=function ()
{
//code
};
variableName();

Code: AnonymousWithoutParam.js

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">Anonymous function without parameter</h1>
</font>
<script>
var names=function()
{
document.write("I am Paramesh <br>");
document.write("I am Amar <br>");
document.write("I am Sri");
};
names();
</script>
</body>
</html>

Output:

Javascript Anonymous Function eg1.1

Explanation:

2. Anonymous Function with Parameters

Syntax:

var division=function (a,b)
{
//code
};
division(20,10);

Code: AnonymousWithParam.js

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">Anonymous function with parameters</h1>
</font>
<script>
var division=function(a,b)
{
divisionOutput=a/b;
document.write("Division of "+b+" and "+a+" is :"+divisionOutput);
};
division(20,10);
</script>
</body>
</html>

Output:

Javascript Anonymous Function eg2

Explanation:

3. Anonymous Function with Return Statement

Syntax:

function functionName(parameters1,parameters2,)
{
return function(a,b,..)
{
return //required logic;
};
}
var out= functionName(1,2,);
document.write(out(1,2,.));

Code: Multiplication.js

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">Anonymous function with Return Statement</h1>
</font>
<script>
var multiplication=function(x)
{
return function(y)
{
return x*y;
};
}
var out=multiplication(19);
document.write("Muliplication of 2 number is :"+out(18));
</script>
</body>
</html>

Output:

eg3

Explanation:

eg3.1

4. Passing Anonymous Function as Argument

Syntax:

function functionName(parameter)
{
return parameter();
}
document.write(functionName ()
{
return
//code;
}));

Code: PassFunctionAsArgument.js

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">Passing Anonymous Function as Argument</h1>
</font>
<script>
function getMyClass(myClass)
{
return myClass(); //calling anonymous function
}
document.write(getMyClass(function (){
return "I am from Class 10th";
}));
</script>
</script>
</body>
</html>

Output:

eg4

Explanation: