Arrow Functions in JavaScript

Arrow Functions

These expressions lexically bind the this value while using less syntax than a typical function expression. Arrow functions are always anonymous.

Here are some basic examples of arrow function syntax:

(parameter) => {statements}
parameter => {statements}
parameter => expression
parameter => {return expression}

(param1, param2, ..., paramN) => {statements}
(param1, param2, ..., paramN) => expression
(param1, param2, ..., paramN) => {return expression}

EXAMPLE

Let's look at some simple ways to apply this syntax:

Run
Output

Using Arrow Functions

Let's look at some ways we can use arrow functions to make our code shorter.

EXAMPLE

Sum the Elements of an Array

While we can certainly iterate over an array and sum each value, we can also use the reduce function.

Run
Output

Now, let's reduce the length of our code using an arrow function:

Run
Output

Let's further reduce it by getting rid of the return:

Run
Output

EXAMPLE

Find the Length of Strings in an Array

Let's take an array of strings and use it to create a new array containing the respective lengths of its elements.

Run
Output

Now, let's reduce the length of our code using an arrow function:

Run
Output

EXAMPLE

Find Array Elements Greater Than a Value

Let's find all the elements in an array that are greater than and add them to a new array.

Run
Output

Now, let's reduce the length of our code using an arrow function:

Run
Output

 
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score