Skip to content

Object Literals

Object literals use {} syntax to create objects with their properties inline. They are the most common way to create objects in JavaScript.

const person = {
name: 'Alice',
age: 25,
greet() {
return `Hello, I'm ${this.name}`;
}
};
const name = 'Alice';
const age = 25;
const person = { name, age };
// Equivalent to: { name: name, age: age }
const key = 'dynamicField';
const obj = {
[key]: 'value',
['computed_' + key]: 42
};
// { dynamicField: 'value', computed_dynamicField: 42 }
  • Object literals are {} with property:value pairs
  • Use shorthand when variable name matches property name
  • Use [expression] for dynamic property names