jskatas.org Continuously Learn JavaScript. Your Way.

Arrow functions: basics

Arrow functions are a more convinient and shorter way to write a function.

Arrow functions

are shorter to write, instead of function(){} write () => {}
var func = function(){}; assert.equal('' + func, '() => {}');
instead {} use an expression, as return value
var func = () => {}; assert.equal(func(), 'I return too');
one parameter can be written without parens
var func = p => param - 1; assert.equal(func(25), 24);
many params require parens
var func = param => param + param1; assert.equal(func(23, 42), 23+42);
the function body needs parens to return an object
var func = () => {iAm: 'an object'}; assert.deepEqual(func(), {iAm: 'an object'});

Related Katas

function API

Arrow functions

Async Function

  • basics

Difficulty Level

BEGINNER

First Published

19 March 2015

Stats

5 tests to solve