JavaScript Arrow Functions

Back to JS page

Table of Contents

Arrow Function Syntax

Arrow functions were introduced in ES6 (2015).

Arrow functions allow us to write shorter function syntax:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Arrow Function Syntax</h4>
<p id="demo"></p>

<script>
// Traditional function expression
const hello1 = function() {
  return "Hello World!";
};

// Arrow function
const hello2 = () => {
  return "Hello World!";
};

document.getElementById("demo").innerHTML = hello2();
</script>

</body>
</html>

Example 1

Result View Example

Shorter Syntax

If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Shorter Syntax</h4>
<p id="demo"></p>

<script>
// Traditional function
const add1 = function(a, b) {
  return a + b;
};

// Arrow function (shorter)
const add2 = (a, b) => a + b;

document.getElementById("demo").innerHTML = "5 + 3 = " + add2(5, 3);
</script>

</body>
</html>

Example 2

Result View Example

Arrow Functions with One Parameter

With one parameter, parentheses are optional:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Arrow Functions with One Parameter</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
// With parentheses
const square1 = (x) => x * x;

// Without parentheses (one parameter only)
const square2 = x => x * x;

document.getElementById("demo1").innerHTML = "With (): " + square1(5);
document.getElementById("demo2").innerHTML = "Without (): " + square2(5);
</script>

</body>
</html>

Example 3

Result View Example

Arrow Functions Return Value by Default

Arrow functions return value by default when using the shorthand syntax without curly brackets:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Arrow Functions Return Value by Default</h4>
<p id="demo"></p>

<script>
const multiply = (a, b) => a * b;
document.getElementById("demo").innerHTML = "6 * 7 = " + multiply(6, 7);
</script>

</body>
</html>

Example 4

Result View Example

Arrow Functions with No Parameters

Even with no parameters, we still need empty parentheses:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Arrow Functions with No Parameters</h4>
<p id="demo"></p>

<script>
const greeting = () => "Hello World!";
document.getElementById("demo").innerHTML = greeting();
</script>

</body>
</html>

Example 5

Result View Example

Arrow Functions Are Not Declarations

Arrow functions are expressions, NOT declarations. They cannot be hoisted:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Arrow Functions Are Not Declarations</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
// Function declaration can be called before definition
document.getElementById("demo1").innerHTML = "Declaration: " + myFunction(5);

function myFunction(x) {
  return x * 2;
}

// Arrow function is an expression - must be defined before use
const myArrow = (x) => x * 2;
document.getElementById("demo2").innerHTML = "Arrow: " + myArrow(5);
</script>

</body>
</html>

Example 6

Result View Example

Document

Document in project

You can Download PDF file.

Reference