LearnThatStack Ace your next interview
JavaScript · question
Question 9 of 126

How do you create objects in JavaScript?

beginner
← All JavaScript questions
Re-explain

The usual way is an object literal, such as { name: "John" }. You can also call new Object(), use Object.create() to choose the new object's prototype, or call a constructor function with new.

  • An object literal is the shortest form and covers most cases.
  • new Object() does the same thing with more code, so it is rarely used.
  • Object.create(proto) makes an object whose prototype is proto, and Object.create(null) makes one with no prototype at all, which suits a plain dictionary.
  • A constructor function called with new builds many objects of the same shape, which is what the class syntax does today.
// Object literal
const obj1 = { name: "John", age: 30 };
// Object constructor
const obj2 = new Object();
obj2.name = "John";
// Object.create()
const obj3 = Object.create(null);
// Constructor function
function Person(name) { this.name = name; }
const obj4 = new Person("John");
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What does Object.create(null) give you that an empty literal {} doesn't, and when would you use it? It inherits no keys like toString or constructor, so any string is a safe key. Also name the cost: the object has no built-in methods.
  • Walk me through what happens, step by step, when you call new Person('John'). Name the steps. A new object is linked to Person.prototype, the function runs with this set to it, and it is returned unless the function returns its own object.
  • How is Object.create() different from calling a constructor with new? Object.create sets the prototype directly and runs no setup code. A constructor runs code to set fields and links the object to Constructor.prototype.
  • How does the class syntax relate to constructor functions? Classes use the same prototype mechanism with cleaner syntax. A strong answer adds a real difference, such as a class throwing when called without new.
  • When would you pick a factory function over a class for creating objects? There is no new or this to get wrong, and closures give private state. The cost is that each object gets its own copy of every method.

What you can say

  1. Most of the time I create objects with an object literal, curly braces with the properties inside, because it's the shortest form and covers most cases.
  2. You can also write new Object() and add properties after, but it does the same thing with more code, so I rarely see it.
  3. Object.create lets me pick the new object's prototype, and Object.create(null) makes an object with no prototype at all, which works well as a plain dictionary.
  4. When I need many objects of the same shape, I call a constructor function with new, and today the class syntax does that same job.
  5. The literal is my default. I only use Object.create(null) when I need a clean dictionary, because that object has no toString or hasOwnProperty to call.

Weak answers to avoid

  • Lists four syntaxes, no when-to-use A memorized list shows recall, not judgment. Say the literal is the default, then say when you would use Object.create or a constructor instead.
  • Treats Object.create(null) as the same as {} An empty literal inherits from Object.prototype, so keys like toString already exist on it. Object.create(null) has no prototype, which is why it suits a dictionary.
  • Talks about class as a separate object model Class syntax builds objects the same way a constructor called with new does, through a prototype. Saying so shows you know what runs underneath.
  • Forgets that a constructor needs new Calling Person without new builds nothing. In sloppy mode this is the global object, in strict mode it throws. A class throws if you call it without new.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime