Shorter Object Literals
Property shorthand lets you write { name } instead of { name: name }.
It makes object literals cleaner and less repetitive.
JavaScript Tutorial
Property shorthand reduces repetition when object keys match variable names.
It keeps object literals short and readable.
Shorthand improves readability in object creation and factory functions.
It is widely used in modern JavaScript.
const obj = { name, role }
const obj = { greet() { ... } }const name = "Ava";
const role = "admin";
const user = { name, role };
console.log(user);Shorthand uses variable names as keys.
const user = {
name: "Ava",
greet() {
return "Hi " + this.name;
}
};
console.log(user.greet());Methods can be defined without the function keyword.
Property shorthand lets you write { name } instead of { name: name }.
It makes object literals cleaner and less repetitive.
Methods can also use shorthand: greet() { ... }.
This keeps object definitions compact.
Shorthand is common when creating objects from variables.
It is especially useful in factory functions and reducers.
function createUser(name, role) {
return { name, role };
}
console.log(createUser("Ava", "admin"));Shorthand keeps factory functions concise.
const key = "status";
const value = "active";
const obj = { [key]: value };
console.log(obj);Combine shorthand with computed keys.
Without
const user = { name: name, role: role };With
const user = { name, role };Shorthand only works when variable names match desired keys.
Use explicit keys when it improves clarity.
Remember method shorthand still creates a function.
A shorter way to define object properties when key and variable name match.
No, it is just syntax sugar.
When the key name differs or clarity is needed.
Practice: Create an object with name and age using shorthand.
const name = "Ava";
const age = 22;
// TODO: create object
One Possible Solution
const name = "Ava";
const age = 22;
const user = { name, age };
console.log(user);When variable names match the object keys you want.
It is just shorter syntax; behavior is the same.
Yes, using [key]: value syntax.
Try creating objects with different variable names.