Object Literal Property Value Shorthand

It is common to create JavaScript objects using some local variables with the same names as the object properties ending up with something redundant like this:

const firstName = 'Foo';
const lastName = 'BAR';

const user = {
    firstName: firstName,
    lastName: lastName,
    email: '[email protected]'
};

... but thanks to the Object Literal Property Value Shorthand, it can be written in a shorter manner:

const firstName = 'Foo';
const lastName = 'BAR';

const user = {
    firstName,
    lastName,
    email: '[email protected]'
};

You should define your style guide concerning this syntax.

If the JavaScript ecosystem is quite new for the team, it is better to avoid this syntax which can be misleading.

Refactoring with IntellJ

Refactoring with VSCode

Last updated