JavaScript – Arrow Functions

Lambda expression:

  • Writing a function in a short format.
  • Function: become single line expression.

Anonymous functions:

  • Anonymous – No name
  • Function without name is called “Lambda expression”

Method example:

	function increment(x)
	{
		x=x+1;
		return x;
	}

Lambda expression:

	x => x+1

Lambda expression with zero arguments:

	() => { 
		document.write(“Hello”); 
	};

Lambda expression with input values:

	(x, y) => { 
		document.write(x+y); 
	};

Lambda expression with return values:

	(x, y) => {
		return x+y;
	};

Can be defined simply like:

	(x,y) => x+y ;

Program to define Arrow function to perform multiplication operation:

<html>
	<body>
		<h1> Lambda expression / Arrow Function </h1>
		<p> Defining a function  as an expression in single line </p>
		<p> Name not required - function keyword not required </p>
		<p> Return type not required </p>
		<p id="res"> </p>
		<script>
			var multiply= (x, y) => x*y ;
			document.getElementById("res").innerHTML = multiply(3,5);
		</script>
	</body>
</html>

Arrow function can be define in more than one statement using {  }

<!doctype html>
<html>
	<body>
		<h1> Lambda expression / Arrow Function </h1>

		<p> Defining a function  as an expression in single line </p>
		<p> Name not required - function keyword not required </p>
		<p> Return type not required </p>

		<p id="res"> </p>

		<script>
			var z = (x, y) => {
				var sum = x*y ;
				return sum
			}
			document.getElementById("res").innerHTML = z(5,5);
		</script>
	</body>
</html>
Scroll to Top