JavaScript Enhanced Object Literals

Why should you care about Enhanced Object Literals?

These features remove repetitive object code and make your intent clearer. You will use them regularly when building configs, API payloads, and reusable components.

ES6 (ECMAScript 2015) introduced shorthand improvements for writing objects in JavaScript. These are called enhanced object literals, and they help you write cleaner code with less repetition.

The three main features covered in this tutorial are property shorthand, method shorthand, and computed property names.

Optional chaining is a related modern JavaScript feature, but it is not object-literal shorthand. It changes how you read or call values after an object already exists.

Start with the ES5 idea: an object literal is a value written with curly braces, containing named properties and functions. ES6 does not change what the object stores; it gives you shorter syntax for creating the same kinds of properties.

ES5 object-literal syntax compared with the equivalent ES6 enhanced syntax.
Feature ES5 syntax ES6 syntax What changed?
Property shorthand { theme: theme } { theme } When the variable and property have the same name, write the name once.
Method shorthand { greet: function() { ... } } { greet() { ... } } Remove the property colon and function keyword.
Computed property names var object = {}; object[key] = value; { [key]: value } Use brackets while creating the object to evaluate the property name.
Enhanced object literals
Three ways to write properties inside an object.
Property shorthand: Use a variable name as the property key.
Method shorthand: Define a method without the function keyword.
Computed property: Use the result of an expression as the key.
const theme = "dark";
const key = "layout";
const config = {
  theme,
  [key]: "grid",
  describe() { return this.theme; },
};
config.describe(); // "dark"

The ES5 forms are still valid JavaScript, so learn to recognise them when reading older code. The ES6 forms mainly reduce repetition; they do not automatically make an object immutable, private, or safer.

When a variable name and the object property name are exactly the same, you do not need to repeat yourself. You can just write the variable name once.

javascript

let theme = "dark";
let layout = "grid";

// Old way
let config1 = { theme: theme, layout: layout };

// ES6 shorthand (same name, write once)
let config2 = { theme, layout };

console.log(config2); // Output: { theme: 'dark', layout: 'grid' }

This is especially handy when building objects from function parameters or variables that already have the right names.

Write the name once
Use shorthand when the key and variable match.
Explicit: { theme: theme, layout: layout }
Shorthand: { theme, layout }
const theme = "dark";
const layout = "grid";
const config = { theme, layout };
config; // { theme: "dark", layout: "grid" }
Use an explicit key when the names differ.

Build an object with property shorthand.

Method shorthand removes the function keyword and the colon syntax used in the older object-literal form. You can write the method name and parentheses directly.

javascript

// Old way
let calculator1 = {
  add: function(a, b) {
    return a + b;
  }
};

// ES6 method shorthand
let calculator2 = {
  add(a, b) {
    return a + b;
  }
};

console.log(calculator2.add(3, 4)); // Output: 7

The method shorthand keeps the object cleaner. It works like a regular method definition, so this refers to the object when the method is called as calculator2.add(). It is not the same as an arrow function, which does not create its own this.

Define a method inline
Call the method the same way with either syntax.
Function property: add: function (left, right) { ... }
Method shorthand: add(left, right) { ... }
const calculator = {
  add(left, right) {
    return left + right;
  },
};
calculator.add(3, 4); // 7

Define and call an object method with shorthand syntax.

Computed property names let you calculate the property key while creating the object. Put the expression inside square brackets [].

javascript

let key = "color";

let car = {
  brand: "Toyota",
  [key]: "red"   // computed property name
};

console.log(car); // Output: { brand: 'Toyota', color: 'red' }

You can also use expressions inside the brackets:

javascript

let prefix = "panel";

let layout = {
  [prefix + "1"]: "sidebar",
  [prefix + "2"]: "navbar"
};

console.log(layout); // Output: { panel1: 'sidebar', panel2: 'navbar' }

The expression inside [] is evaluated when the object literal is created and converted to a property key. A number becomes a string key, while a Symbol remains a symbol key.

Compute a property key
Brackets evaluate the expression inside them.
Variable: key
Evaluates to: "color"
Property: color: "red"
const key = "color";
const car = { brand: "Toyota", [key]: "red" };
car.color; // "red"
Without brackets, key would be the literal name.

Calculate an object property name from a variable.

Optional chaining (?.) is a related modern JavaScript feature, but it is not part of enhanced object literal syntax. It safely accesses a property or calls a method when the value before ?. may be null or undefined.

javascript

const config = {
  display: {
    columns: 3
  }
};

console.log(config.display?.columns); // 3
console.log(config.toolbar?.position); // undefined

?? provides a fallback only when a value is null or undefined. It is separate from enhanced object literal syntax and from optional chaining.

Practical Pattern: Combining Modern Object Syntax

Enhanced object syntax is useful when an object combines values, behavior, and dynamic keys. This example also uses optional chaining and nullish coalescing for API data; those are related modern JavaScript features, not enhanced object literal features.

javascript

function createPayload(user, fieldName, fieldValue) {
  const { id, name } = user; // destructure the values we need

  return {
    id,                         // property shorthand
    name,
    [fieldName]: fieldValue,    // computed property name
    describe() {                // method shorthand
      return `${this.name} (${this.id})`;
    },
    avatarUrl: user.profile?.avatar ?? "/default-avatar.png"
  };
}

const payload = createPayload(
  { id: 7, name: "Mina", profile: {} },
  "status",
  "active"
);

console.log(payload.describe()); // Mina (7)
console.log(payload.avatarUrl);   // /default-avatar.png
Identify each enhanced object feature in the payload.
SyntaxJobWhy it helps
{ id, name }Property shorthandRemoves repeated names when variables already match the API fields.
describe() {}Method shorthandKeeps object behavior readable; when called as object.method(), the method uses that object's this value.
[fieldName]Computed propertyCreates a key from runtime data.
?. with ??Related syntaxSafely reads missing nested data and provides a fallback without replacing valid falsy values.
Build a payload object
Combine shorthand, a computed key, and a method.
id, name: Copy the bindings into matching properties.
[fieldName]: Use the provided name for the extra property.
describe(): Read the name from the payload object.
function createPayload(user, fieldName, value) {
  const { id, name } = user;
  return {
    id, name,
    [fieldName]: value,
    describe() { return this.name; },
  };
}
const user = { id: 7, name: "Ada" };
const payload = createPayload(
  user, "role", "admin",
);
payload.role; // "admin"
JSON.stringify() omits function-valued properties.
Checkpoint: explain which parts create the object, which part creates behavior, which key is dynamic, and which value is optional before using this pattern in an API request.

Combine shorthand properties, methods, and normal access.

  • Property shorthand: when the variable name matches the property name, write it once: { theme } instead of { theme: theme }.
  • Method shorthand: skip the function keyword inside objects: greet() {} instead of greet: function() {}.
  • Computed property names: use square brackets to set property names from expressions: { [key]: value }.
  • Related syntax: optional chaining (?.) safely accesses a value when the previous value may be null or undefined.
  • ?? provides a fallback only when a value is null or undefined.

What's next? Now that you can write cleaner object code, let's explore how to find and update elements in a page in the DOM tutorial.

Videos for this topic will be added soon.

Reviewed by

SimplyJavaScript Editorial Team

Technical editors and JavaScript educators with hands-on experience building frontend projects, writing learning material, and reviewing tutorials for clarity, accuracy, and beginner-friendly guidance.