4

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)?

2
  • 4
    The { after the => is interpreted as the beginning of a code-block, not the beginning of an object literal, use (st, et) => ({ startTime: st, endTime: et }); for objects Commented Dec 31, 2019 at 4:11
  • Thanks! When to use return in es6 was helpful too as marked. Commented Dec 31, 2019 at 4:19

1 Answer 1

10

You can use () to wrap it like this:

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

console.log(makeRange(1, 2));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.