Curriculum
Course: React
Login

Curriculum

React

Text lesson

ES6 Arrow Functions

Arrow Functions

Arrow functions enable a more concise syntax for writing functions.

Example

Before:

hello = function() {
 return "Hello World!";
}

Example

With Arrow Function:

hello = () => {
 return "Hello World!";
}

 

It gets even shorter! If the function has a single statement that returns a value, you can omit both the curly braces and the return keyword.

Example

Arrow functions return a value by default.

hello = () => “Hello World!”;

Note: This applies only if the function contains a single statement.

If you have parameters, include them within the parentheses.

Example

Arrow Function with Parameters

hello = (val) => “Hello “ + val;

In fact, if you have just one parameter, you can also omit the parentheses.

Example

Arrow Function Without Parentheses

hello = val => “Hello “ + val;