Enhanced Object Literals
ES6 made writing objects shorter and more flexible. Three small features — shorthand properties, shorthand methods and computed keys — appear in almost every modern file.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 28, 2026
Learning objectives#
- Use property shorthand { name } instead of { name: name }
- Write methods with shorthand syntax
- Create keys dynamically with [computed] names
- Build objects from lists with computed keys
Explanation#
Property shorthand: if a variable has the same name as the key, write it once — { name, age } means { name: name, age: age }.
Method shorthand: greet() { … } instead of greet: function () { … }. These methods get a normal this.
Computed property names: put an expression in square brackets to use its value as the key — { [field]: value } or { [`${field}Error`]: msg }.
Together they make it easy to build objects from variables and loops, e.g. form state where the key comes from the input’s name.
Syntax#
const user = {
name, // shorthand property
greet() { }, // shorthand method
[key]: value, // computed key
};
Code example: Shorter, smarter objects#
const name = 'Asha', age = 24;
const user = {
name,
age,
greet() {
return `Hi, I am ${this.name}`;
},
};
console.log(user.greet());
const field = 'email';
const form = { [field]: 'asha@example.com', [`${field}Verified`]: true };
console.log(form);
const stats = ['likes', 'shares'].reduce((acc, key) => ({ ...acc, [key]: 0 }), {});
console.log(stats);
Hi, I am Asha
{ email: 'asha@example.com', emailVerified: true }
{ likes: 0, shares: 0 }Step-by-step explanation#
- { name, age } copies the variables in with matching keys.
- greet() is a shorthand method, so this is the user object.
- [field] becomes the key "email" — the value of the variable, not the word field.
- A template literal inside [ ] builds "emailVerified".
- reduce + computed keys turns a list of names into an object of counters.
Real-world use case#
React forms update state with setForm({ ...form, [e.target.name]: e.target.value }) — one handler for every input.
Common mistakes#
{ field: 'x' } creates a key literally called field. Use [field] to use the variable’s value.
greet: () => this.name does not see the object. Use the method shorthand greet() { … }.
{ total } throws ReferenceError if no total variable exists.
Best practices#
- Use property shorthand whenever names match
- Use method shorthand for object methods
- Use computed keys for dynamic form fields and lookups
- Keep computed key expressions simple
Remember this#
{ name } = { name: name }.
[expression]: value → the key is computed at runtime.
Methods in objects: greet() { }, not greet: () => { }.
Practice exercise#
Your task: Write a function handleChange(form, inputName, value) that returns a new form object with that field updated.
Need a hint?
Show solution
const handleChange = (form, inputName, value) => ({ ...form, [inputName]: value });
console.log(handleChange({ name: '', email: '' }, 'email', 'a@b.com'));
// { name: '', email: 'a@b.com' }
Mini quiz#
What is printed?
const key = 'color';
const o = { [key]: 'red' };
console.log(Object.keys(o));
Want more? Practise all ES6+ MCQs · ES6+ interview questions
My notes#
Saved in this browser.