Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow function "expression expected" syntax error

I want to transform this code:

var formatQuoteAmount = function (tx) {     return Currency.toSmallestSubunit(tx.usd, 'USD'); }; var quoteAmounts = res.transactions.map(formatQuoteAmount); 

into an anonymous arrow function. I've written this:

var quoteAmounts = res.transactions.map(tx => Currency.toSmallestSubunit(tx.usd, 'USD')); 

I get expression expected syntax error at the arrow. I looked up the default syntax here and seems like the syntax of my code is correct. Any ideas what the problem might be?

I have it working with this syntax:

    var quoteAmounts = res.transactions.map(function (tx) {         return Currency.toSmallestSubunit(tx.usd, 'USD')     }); 

but I want to make it a one-liner, with an arrow-function.

Running on node v5.3.0

like image 297
Milkncookiez Avatar asked Feb 22 '16 09:02

Milkncookiez


People also ask

What is the correct syntax for an arrow function?

An arrow function looks similar to a function expression — it's just shorter. Again we assign an anonymous function to a named variable. The syntax of the arrow function consists of zero or more parameters , an arrow => and then concludes with the function statements .

What is expression expected error?

The "Expression expected" TypeScript error occurs when we have a syntax error in our code or our code editor is using an older version of TypeScript. To solve the error, make sure to correct and syntax errors and use a recent version of the TypeScript compiler.

Is arrow function syntax sugar?

Arrow function syntax is wonderful syntactic sugar that removed the need for the following: the return keyword (for one line functions)

What are the disadvantages of arrow function?

An Arrow function should not be used as methods. An arrow function can not be used as constructors. An arrow function can not use yield within its body. Arrow function cannot be suitable for call apply and bind methods.


1 Answers

I had the error expression expected reported by Webstorm when editing a Node.js program. In this case the solution is to set the language version to a version that supports this feature.

enter image description here

like image 55
Joe23 Avatar answered Oct 15 '22 10:10

Joe23