Skip to main content

How to Use Shorthand Property Names in JavaScript

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

You often build an object from variables that already have the right names: a name variable becomes a name property. JavaScript lets you write that name once, so { name: name } shrinks to { name }.

Syntax​

Syntax
const <object> = { <variable>, <otherVariable> };
PartWhat it does
<variable>Creates a property with the variable's name and the variable's value.
{ <variable> }Means exactly the same as { <variable>: <variable> }.

MDN's object initializer reference first builds an object as a: a, b: b, c: c, then says: "There is a shorter notation available to achieve the same", and writes const o = { a, b, c };.

The Example​

The example builds the same user object twice, once with the long form and once with the shorthand, and prints both.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write Both Forms​

Create a file named main.js:

main.js, which sets name to Ada and role to admin, builds longForm by writing each property name followed by its variable, builds shortForm by writing only the names, and prints both.

main.js
const name = 'Ada';
const role = 'admin';

const longForm = { name: name, role: role };
const shortForm = { name, role };

console.log(longForm);
console.log(shortForm);

In longForm, each property spells out its name and then the variable it takes its value from. In shortForm, each name does both jobs.

Step 2: Run It​

Terminal
node main.js
Output
{ name: 'Ada', role: 'admin' }
{ name: 'Ada', role: 'admin' }

A zsh terminal where node main.js prints the same object twice: name Ada, role admin.

Both objects are identical. The command is the same in zsh, bash and PowerShell.

Mixing Both Forms​

Shorthand and normal properties can sit in the same object, which helps when only some names match:

Mixed
const user = { name, role, active: true };

Common Mistakes​

MistakeWhat happensDo this instead
Using shorthand for a variable that doesn't exist, as in { nickname }ReferenceError: nickname is not definedDeclare the variable first, or write nickname: value
Expecting shorthand to rename{ name } always makes a property called nameWrite fullName: name when the property needs another name

Troubleshooting​

SymptomLikely causeFix
ReferenceError: … is not defined at an object literalA shorthand property points at a missing variableCheck the variable's spelling and that it's declared above
A property has an unexpected nameShorthand used the variable's nameUse newName: variable for that property

FAQ​

It copies the value at the moment the object is created. Changing the variable afterwards doesn't change the property.

Is there a shorthand for methods too?​

Yes, a method definition: { greet() { ... } } gives the object a greet method without writing greet: function () { ... }.

Conclusion​

When a property and a variable share a name, write the name once: { name, role } builds the same object as { name: name, role: role }.

Additional Resources​