Property Shorthand Syntax
What you'll learn: How to write cleaner object literals when your property names match your variable names.
The Long Way vs. The Short Way
When creating object literals, you often find yourself doing this:
const name = "Alice";
const age = 30;
const city = "Boston";
const user = {
name: name,
age: age,
city: city
};
Notice the repetition? You're writing name: name, age: age, and so on. JavaScript sees this pattern all the time, so it gives you a shortcut.
Property Shorthand in Action
When the variable name matches the property key exactly, you can simply omit the value part:
const name = "Alice";
const age = 30;
const city = "Boston";
const user = {
name,
age,
city
};
console.log(user); // { name: "Alice", age: 30, city: "Boston" }
The result is identical — JavaScript automatically uses the variable's value as the property value.
When to Use It
Use shorthand whenever the variable name and desired property name are the same. You can mix shorthand and regular syntax:
const username = "alice123";
const score = 95;
const player = {
username, // shorthand
score, // shorthand
level: 5, // regular syntax
active: true // regular syntax
};
This only works when you have a variable available. You cannot use shorthand with literal values or computed property names directly.
Key Takeaway: Property shorthand lets you write { name } instead of { name: name } when the variable name matches the property key, reducing redundancy and making your object literals cleaner.