Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning JSON Object in Javascript ES6 [duplicate]

I'm relatively new to ES6 Syntax. I had written a piece of code like follows:

const makeRange = (startTime, endTime) => {
    return { startTime: startTime, endTime: endTime };
};

This worked fine, though I thought I shouldn't need the function braces ({ ...body ...}) for a one line return. The Following code:

const makeRange = (st, et) => { startTime: st, endTime: et };

As pointed by IntelliJ or Webstorm: "Expression Statement is not assignment or call".

How should I do it correctly(if it is valid)?

like image 696
Prashant Vishwakarma Avatar asked Aug 31 '25 02:08

Prashant Vishwakarma


1 Answers

You can use () to wrap it like this:

const makeRange = (st, et) => ({ startTime: st, endTime: et });

console.log(makeRange(1, 2));
like image 109
Tân Avatar answered Sep 04 '25 09:09

Tân