Arrow Functions

Arrow functions are another way to declare functions.

// using function keyword
function name() 
{
    return "Mary"
}


// using arrow function
name = () => 
{
  return "Mary"
}

 

If the function has only one statement, and the function returns a value, we can remove the curly brackets and the return keyword, as shown below:

name = () => "Mary"

 

Note that, if we include curly brackets (i.e. the function is in a block), then we must also include the return keyword, as shown below:

name = () => {return "Mary"}

 

If a function has only one parameter, we can remove the round brackets

/* With round brackets */
square = (number) => number * number

/* Without round brackets */
square = number => number * number

 

An arrow function cannot contain a line break between its parameters (i.e. the round brackets) and its arrow.

/* Syntax error */
result = ()
    => 1


/* This is allowed */
result = () =>
    1

Search online and research why we should not replace every normal function with an arrow functions. Then:

 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>