Skip to main content

How to Create an Object Literal in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

An object groups related values under names, such as a book's title, author and year. The quickest way to make one in JavaScript is an object literal: the values written out between curly braces, right where you need them.

Syntax​

Syntax
const <object> = {
<name>: <value>,
<name>: <value>,
};
PartWhat it does
{ ... }Creates a new object.
<name>: <value>One property: a name, a colon and its value. Commas separate the properties.
<object>A variable that holds the object.

MDN's object initializer reference defines it: "An object initializer, sometimes also called an object literal, is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({})."

The Example​

The example describes a book with three properties, reads two of them, adds a fourth and prints the whole object.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Object​

Create a file named main.js:

main.js, which creates a book object with title Dune, author Frank Herbert and year 1965, prints the title and the author, adds pages 412 and prints the book.

main.js
const book = {
title: 'Dune',
author: 'Frank Herbert',
year: 1965,
};

console.log(book.title);
console.log(book['author']);

book.pages = 412;
console.log(book);
  • book.title reads a property with dot notation.
  • book['author'] reads one with bracket notation, which takes the name as a string.
  • book.pages = 412 adds a property that wasn't there.
  • The comma after 1965 is allowed. It makes adding the next property a one-line change.

Step 2: Run It​

Terminal
node main.js
Output
Dune
Frank Herbert
{ title: 'Dune', author: 'Frank Herbert', year: 1965, pages: 412 }

A zsh terminal where node main.js prints Dune, Frank Herbert and the book object with title, author, year and pages.

The new pages property appears at the end of the object. The command is the same in zsh, bash and PowerShell.

Dot or Brackets?​

Use dot notation when you know the name as you write the code. Use brackets when the name has a space or a dash, or when it's in a variable:

Bracket notation
const person = { 'first name': 'Ada' };
console.log(person['first name']); // Ada

Common Mistakes​

MistakeWhat happensDo this instead
Reading a property that doesn't exist, such as book.isbnYou get undefined, with no errorCheck the name, or give the object that property
Writing = instead of : inside the bracesSyntaxError: Invalid shorthand property initializerWrite name: value
Using dot notation for a name with a spaceA SyntaxError, such as missing ) after argument list inside console.log(...)Use object['first name']

Troubleshooting​

SymptomLikely causeFix
undefined where you expected a valueThe property name is misspelled or missingCompare the name with the object's printout
SyntaxError: Unexpected identifier 'author'The comma before that property is missingPut a comma after each property

FAQ​

Can I change a property of an object in a const variable?​

Yes. const stops the variable from pointing at another object, but the object's properties can still change, as book.pages = 412 shows.

How do I see every property name?​

Object.keys(book) returns them as an array: ['title', 'author', 'year', 'pages'].

Conclusion​

Curly braces with name: value pairs create an object on the spot. Read its properties with object.name or object['name'], and assign to a new name to add one.

Additional Resources​