samsos samsos - 6 months ago 29
Javascript Question

JS: Using 'return' in arrow function syntax

I'm currently trying to return every first character of each string in the

array and storing in a new array called
using shorthand arrow function syntax. But, I run into the following error.


/home/ccuser/workspace/learn-javascript-iterators-E3/main.js:5 => return animal[0]);
SyntaxError: Unexpected token return


let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; => return animal[0]);


Why does this occur? and how can resolve this error?

Answer Source

The expression in an arrow function is the value for return.

In a concise body, only an expression is needed, and an implicit return is attached. In a block body, you must use an explicit return statement.

You could just remove the word return.

var secretMessage = => animal[0]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download