JavaScript object literals: syntax, shorthand, spread, and JSON

Create objects with property pairs, shorthand keys, computed keys, methods, spread, and JSON.parse — all from object literal syntax.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript object literals: syntax, shorthand, spread, and JSON

In JavaScript, a literal is a value you write directly in source code (numbers, strings, arrays, objects). An object literal is the { ... } form: a list of keys and values that creates a plain object in one expression.

This article walks through the main syntax features and small examples you can paste into a console or Node.

For copying and merging objects in depth, see the spread operator in JavaScript.

Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).


Quick reference

Feature Example
Fixed keys { name: "Amit", age: 30 }
Shorthand when names match const x = 1; ({ x }){ x: 1 }
Computed keys { [expr]: value }
Concise methods { add(a, b) { return a + b; } }
Copy / overlay { ...base, extra: true }
From JSON text JSON.parse('{"a":1}')

What object literals give you

  1. Property–value pairs — Each key is a string (or Symbol); values can be any type, including nested objects.
  2. Shorthand property names (ES2015) — When the variable name matches the property name, write { x, y } instead of { x: x, y: y }.
  3. Computed property names (ES2015) — Use [expression] as the key so the name is evaluated at runtime.
  4. Method definitions (ES2015) — Write methodName() { ... } instead of methodName: function () { ... }.
  5. Spread in object literals (ES2018)...other copies enumerable own properties from another object into the new one.
  6. JSONJSON.stringify / JSON.parse exchange data with the wire; object literals are not JSON (JSON has no functions, trailing commas are forbidden in strict parsers, keys are always double-quoted in the string form).

One object combining shorthand, a method, computed keys, and spread

Define otherPerson before you spread it into another literal. The bracket key uses `address_${1 + 2}` in the source, which evaluates to the property name address_3; spread copies city and role onto the same object.

javascript
const otherPerson = { city: "Boston", role: "editor" };

const person = {
  name: "John",
  age: 30,
  speak() {
    console.log("Hello");
  },
  [`address_${1 + 2}`]: "New York",
  ...otherPerson,
};

console.log(
  person.name,
  person.age,
  person.address_3,
  person.city,
  person.role,
);
person.speak();
Output

You should see one line: John 30 New York Boston editor, then a second line: Hello.


Examples

Simple properties and dot access

Braces create the object; dot notation reads fixed string keys.

javascript
const person = {
  name: "Amit",
  age: 30,
  gender: "male",
};
console.log(person.name, person.age, person.gender);
Output

You should see one line: Amit 30 male.

Methods on the object

Concise method syntax stores functions on the object like any other property.

javascript
const calculator = {
  add(a, b) {
    return a + b;
  },
  subtract(a, b) {
    return a - b;
  },
};
console.log(calculator.add(1, 2));
console.log(calculator.subtract(5, 3));
Output

You should see two lines, in order: 3, then 2.

Shorthand property names

When a variable name matches the key you want, omit the : value repetition.

javascript
const x = 5;
const y = 10;
const coordinates = { x, y };
console.log(coordinates.x, coordinates.y);
Output

You should see one line: 5 10.

Computed property names

Bracket expressions in the key position are evaluated when the literal runs.

javascript
const key = "name";
const obj = {
  [key]: "Amit",
  ["age_" + 10]: 30,
};
console.log(obj.name, obj.age_10);
Output

You should see one line: Amit 30.

Spread to clone and add fields

Spread copies enumerable own properties from person, then you add or override keys in the same literal.

javascript
const person = { name: "Amit", age: 30 };
const newPerson = { ...person, gender: "male" };
console.log(newPerson.name, newPerson.age, newPerson.gender);
Output

You should see one line: Amit 30 male.

From JSON string to object

JSON.parse turns a JSON string into a plain object; keys become string properties on the result.

javascript
const personJSON = '{"name": "Amit", "age": 30}';
const person = JSON.parse(personJSON);
console.log(person.name, person.age);
Output

You should see one line: Amit 30.

For safe parsing and revivers, see parse JSON in JavaScript.


Summary

Object literals are the default way to build plain data objects in JavaScript: readable, composable with spread, and aligned with modern syntax (shorthand, computed keys, concise methods). Treat JSON as a serialization format; build and edit data with literals and JSON.parse / JSON.stringify at the boundaries.


References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …