JavaScript Object Literal Shorthand with ECMAScript 2015

Photo by Markus Spiske on Pexels.com

Last Friday, I hit an ESLint error as below:

335:13  error  Expected property shorthand        object-shorthand

Per the “object-shorthand” rule’s documentation, there is a syntactic sugar for defining object literal methods and properties in the ECMAScript 2015 (ES6).

Quite often, when declaring an object literal, property values are stored in variables whose names are equal to the property names. For example:

const timeout = 30000;

const options = { timeout: timeout };

There is a shorthand for this situation:

const timeout = 30000;

const options = { timeout };

References

Leave a comment