How to Create an Object in JavaScript

Use objects when related values belong together under named keys. Objects are the standard way to model users, products, settings, API payloads, and grouped UI state.

What you’ll build or solve

You’ll learn how to create an object in JavaScript using object literals. You’ll also know how to add properties, nested values, and methods.

When this approach works best

This approach is the right choice when values describe one thing with multiple named attributes.

Common real-world scenarios include:

  • User profiles
  • Product details
  • App settings
  • Form data
  • API request bodies

This is a bad idea when order matters more than named keys. In that case, use an array instead.

Prerequisites

You only need:

  • A JavaScript file or browser console
  • Basic variables and arrays knowledge

Step-by-step instructions

Step 1: Create an object with key-value pairs

Use curly braces and define each property as key: value.

JavaScript

const user = {
  name: "Alex",
  age: 30,
  isActive: true
};

console.log(user);

You can mix different value types, including arrays and nested objects.

JavaScript

const product = {
  title: "Wireless Keyboard",
  price: 49,
  tags: ["tech", "office"],
  seller: {
    name: "Jordan",
    rating: 4.9
  }
};

What to look for:

  • Use {} for object literals
  • Keys map to values
  • Values can be strings, numbers, arrays, objects, or functions
  • Great for modeling one real-world thing
  • Property names should describe meaning clearly

Examples you can copy

User profile

JavaScript

const user = {
  username: "mia_dev",
  level: "intermediate"
};

Product object

JavaScript

const course = {
  title: "JavaScript Basics",
  lessons: 24,
  premium: true
};

App settings

JavaScript

const settings = {
  theme: "dark",
  notifications: true
};

Common mistakes and how to fix them

Mistake 1: Using = instead of :

What the reader might do:

JavaScript

const user = {
  name = "Alex"
};

Why it breaks: object properties use : between keys and values.

Corrected approach:

JavaScript

const user = {
  name: "Alex"
};

Mistake 2: Forgetting commas between properties

What the reader might do:

JavaScript

const user = {
  name: "Alex"
  age: 30
};

Why it breaks: JavaScript needs commas between object properties.

Corrected approach:

JavaScript

const user = {
  name: "Alex",
  age: 30
};

Mistake 3: Using an array for named data

What the reader might do:

JavaScript

const user = ["Alex", 30, true];

Why it breaks: the values lose clear meaning without named keys.

Corrected approach:

JavaScript

const user = {
  name: "Alex",
  age: 30,
  isActive: true
};

Troubleshooting

If the object throws a syntax error, check commas and colons.

If property meaning feels unclear, rename the keys.

If values need grouping by name, prefer an object over an array.

If nested data gets hard to read, split it into smaller objects.

Quick recap

  • Use {} to create objects
  • Add key: value pairs
  • Separate properties with commas
  • Use clear property names
  • Great for grouped named data