JavaScript Objects: Creating Objects with Literals, new and Object.create
What a JavaScript object really is (properties, own vs inherited, property attributes), the three ways to create one (object literals, new with a constructor, Object.create), how prototypes and the prototype chain work, and null-prototype objects as safe dictionaries.
kvi5hnuCode Sprout team member7 min readOn this page
Objects are the center of JavaScript. Arrays are objects, functions are objects, dates, maps, regular expressions, errors, DOM elements and promises are all objects. Every value that isn't one of the seven primitives (string, number, bigint, boolean, symbol, null, undefined; article 9) is an object.
Part 6 covers objects in depth: this article explains what an object is and how to create one; the following ones cover reading and writing properties, testing and listing them, copying, JSON, the standard methods, the modern literal syntax, and getters and setters.
What an object is
An object is a collection of properties. Each property has a name (a string or a symbol) and a value (any value, including another object or a function). In that sense an object is a lookup table, the structure other languages call a hash map, dictionary or associative array:
const book = {
title: "Rivers of the World",
pages: 312,
inStock: true,
author: { first: "Lena", last: "Okafor" }, // a nested object
summary() { return `${this.title}, ${this.pages} pages`; }, // a method
};
console.log(book.pages, book.author.last, book.summary());
312 Okafor Rivers of the World, 312 pages
Three more facts complete the picture:
- Objects are mutable and handled by reference.
const copy = bookdoesn't copy anything; both names refer to the same object (article 21). - Objects inherit. Besides its own properties, an object is linked to another object, its prototype, and inherits that object's properties. That's where methods like
toStringcome from, even though you never defined them. - Each property has attributes. Besides its value, every property is writable (can be changed), enumerable (shows up in
for...inandObject.keys) and configurable (can be deleted or redefined), or not. Properties you create normally have all three set to true; many built-in ones don't. Part 14 shows how to control them.
1. Object literals
The most common way to create an object is an object literal: braces containing name: value pairs separated by commas.
const playlist = {
"track name": "Intro", // names that aren't valid identifiers need quotes
class: "pop", // reserved words are fine as property names
2: "two", // numbers become the string "2"
"": "empty", // even the empty string is a valid name
}; // a trailing comma is allowed
console.log(playlist["track name"], playlist.class, playlist[2], playlist[""]);
Intro pop two empty
Points that matter in practice:
- A literal creates a new object every time it's evaluated. A literal inside a function or loop produces a different object on every call or pass, with its values freshly computed:
const makeCounter = () => ({ count: 0 });
console.log(makeCounter() === makeCounter());
false
- Values can be any expressions, evaluated in order:
{ total: price * qty, createdAt: Date.now() }. - Trailing commas are allowed and recommended in multi-line literals: adding a property later changes one line in a diff instead of two.
- The modern literal syntax (shorthand properties like
{ name }, computed names like{ [key]: value }, methods, spread, getters and setters) is covered in articles 52 and 53.
2. new and a constructor
The new operator calls a constructor function, which creates and initializes a new object:
const empty = new Object(); // same as {}
const list = new Array(); // same as []
const now = new Date(); // the current date and time
const lookup = new Map(); // a key/value collection
For plain objects and arrays, literals are shorter and preferred. new is how you create everything that has no literal syntax: dates, maps, sets, errors, URLs, and instances of your own classes (part 9).
Prototypes: where inherited properties come from
Almost every object has a hidden link to another object, its prototype, written [[Prototype]] in the specification. When you read a property the object doesn't have itself, JavaScript looks in the prototype, then in the prototype's prototype, and so on. This series of links is the prototype chain.
Which prototype an object gets depends on how it was created:

- Objects from literals and
new Object()inherit fromObject.prototype. - Objects from
new Constructor()inherit fromConstructor.prototype: anew Date()inherits fromDate.prototype, which in turn inherits fromObject.prototype. That's why a date has bothgetFullYearandhasOwnProperty. Object.prototypeitself has no prototype: its link isnull, the end of every chain.
console.log(Object.getPrototypeOf({}) === Object.prototype);
console.log(Object.getPrototypeOf(new Date()) === Date.prototype);
console.log(Object.getPrototypeOf(Date.prototype) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype));
true
true
true
null
Part 9 builds classes on this mechanism, and part 14 covers changing prototypes.
3. Object.create
Object.create(proto) creates a new, empty object whose prototype is exactly the object you pass:
const greeter = {
greet() { return `Hi, I'm ${this.name}`; },
};
const ana = Object.create(greeter);
ana.name = "Ana";
console.log(ana.greet());
console.log(Object.keys(ana)); // only own properties
console.log(Object.hasOwn(ana, "greet")); // greet is inherited
Hi, I'm Ana
[ 'name' ]
false
Changes to the prototype show up in every object that inherits from it, because inherited properties are looked up live, not copied:
const defaults = { theme: "light" };
const userSettings = Object.create(defaults);
defaults.theme = "dark";
console.log(userSettings.theme);
dark
Object.create(Object.prototype) is equivalent to {}. An optional second argument defines properties with specific attributes (part 14).
Object.create(null): objects with no prototype
Object.create(null) gives an object that inherits nothing: no toString, no hasOwnProperty, no constructor. That sounds useless, but it's exactly what you want for a dictionary whose keys come from data. With a normal object, keys that happen to match inherited names cause bugs:
const words = ["apple", "constructor", "apple"];
const plain = {};
for (const w of words) plain[w] = (plain[w] || 0) + 1;
console.log(plain.constructor); // inherited function + 1 = a string!
const safe = Object.create(null);
for (const w of words) safe[w] = (safe[w] || 0) + 1;
console.log(safe);
function Object() { [native code] }1
[Object: null prototype] { apple: 2, constructor: 1 }
plain.constructor started as the inherited Object function, so + 1 turned it into a string. The null-prototype object has no inherited names to collide with. The trade-off: it can't be converted to a string (String(obj) and template literals throw a TypeError, since there's no toString), and it has no methods, so use the static functions like Object.keys(obj) and Object.hasOwn(obj, key).
For key/value data, Map (part 11) is often an even better choice: keys of any type, a size, guaranteed insertion order and no inherited keys at all.
Guarding an object you pass to other code
Because writes go to the object itself and never to its prototype (next article), passing Object.create(original) to a function lets it read everything in original while any properties it sets land on the wrapper. It's a lightweight guard against accidental modification, not a security boundary; Object.freeze (part 14) or structuredClone (article 21) are stronger.
Which way to create objects?
| Situation | Use |
|---|---|
| A plain data object or configuration | object literal { ... } |
| Many objects with the same shape and methods | a class and new (part 9) |
| Built-in types: dates, maps, sets, errors | new Date(), new Map(), ... |
| An object inheriting from a specific object | Object.create(proto) |
| A dictionary keyed by arbitrary strings | Map, or Object.create(null) |
Common questions
Are arrays and functions really objects?
Yes. typeof [] is "object" and typeof function () {} is "function", but both are objects: you can add properties to them, and they inherit from Array.prototype and Function.prototype, which inherit from Object.prototype. Parts 7 and 8 cover them.
Is {} different from new Object()?
No, they produce the same kind of object. The literal is shorter, can't be affected by someone reassigning a global Object, and is what every style guide recommends (ESLint's no-object-constructor).
Does an object remember the order of its properties?
Mostly. Integer-like keys come first in ascending order, then other string keys in the order they were added, then symbols (articles 42 and 48). Use a Map or an array when you need guaranteed order of arbitrary keys.
Why does console.log show [Object: null prototype]?
That's Node.js telling you the object was made with Object.create(null), so it doesn't behave like an ordinary object. Browsers show it as an object without a [[Prototype]] entry.
Summary
- An object is a collection of properties (name → value), handled by reference, with a prototype it inherits from.
- Each property is writable, enumerable and configurable unless set otherwise.
- Create objects with literals (
{}), withnewand a constructor, or withObject.create(proto). - A literal makes a new object every time it's evaluated.
- Literals inherit from
Object.prototype;new C()objects fromC.prototype;Object.create(p)objects fromp; the chain ends atnull. Object.create(null)makes a prototype-free dictionary with no inherited names;Mapis often better still.
Practice Questions
What does this print?
const a = { n: 1 };
const b = { n: 1 };
const c = a;
console.log(a === b, a === c);
Answer
false true. Two literals create two different objects, even with identical contents; c refers to the same object as a.
What does this print?
const base = { kind: "base" };
const child = Object.create(base);
console.log(child.kind, Object.keys(child).length);
Answer
base 0. kind is inherited from the prototype, so it can be read, but it's not an own property and doesn't appear in Object.keys.
Why is const counts = {} risky for counting words from user text, and what are two safer choices?
Answer
A plain object inherits names like constructor, toString and hasOwnProperty, so counts["constructor"] starts as a function instead of undefined. Use Object.create(null) or, better, a Map.
What does this expression return?
Object.getPrototypeOf(Object.getPrototypeOf([]))
Answer
Object.prototype. An array's prototype is Array.prototype, and Array.prototype inherits from Object.prototype.
Previous: JavaScript Strict Mode, debugger and with: What "use strict" Changes
Next: JavaScript Object Properties: Reading, Writing, Dynamic Keys and Inheritance

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.