Skip to content

Concise arrow functions in JavaScript

Arrow functions were introduced with ES6 as a new syntax for writing JavaScript functions. They let the developers write functions with a simple syntax. All modern browsers support arrow functions.

There are two ways of writing arrow functions: concise and fat. In this post we are focusing only on the concise way.

Advertisements

Syntax

(param1, param2) => do something with param1 and param2

Examples

To implement a function to add two numbers in consise way, we can use arrow functions like this:

//consise arrow function for adding two numbers
const add = (x, y) => x + y; 
Advertisements

The arrow function example above allows us to get the same result with fewer lines of code and approximately half the typing.

//consise arrow function to get even numbers from an array
const even = array.filter(v => v % 2 === 0);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.