Skip to main content

Creating an object

Object literal - the simplest and most common way

javascript
const user = { name: 'Oleh', age: 25, isAdmin: false };

This is the most readable and most widely used way to create objects.

You can add and remove properties dynamically:

javascript
user.city = 'Kyiv'; delete user.isAdmin;

2. Via the new Object() constructor

javascript
const user = new Object(); user.name = 'Oleh'; user.age = 25;

Does the same thing as {}, but is longer and looks "old-fashioned". Nowadays it is almost never used.


3. A constructor function

Before class existed, this is how objects were created "from a template".

javascript
function User(name, age) { this.name = name; this.age = age; } const oleh = new User('Oleh', 25); const alex = new User('Alex', 30); console.log(oleh.name); // "Oleh"

When you call a function with new, JS does the following under the hood:

  1. creates an empty object {},
  2. assigns it to this,
  3. returns that object.

4. ES6 class (syntactic sugar over a constructor function)

javascript
class User { constructor(name, age) { this.name = name; this.age = age; } sayHi() { console.log(`Hello, I am ${this.name}`); } } const oleh = new User('Oleh', 25); oleh.sayHi(); // "Hello, I am Oleh"

Under the hood, class is the same function, but with a more convenient and understandable syntax.


5. A factory function

Instead of class or new, you can simply return an object:

javascript
function createUser(name, age) { return { name, age, sayHi() { console.log(`Hello, ${this.name}`); } }; } const oleh = createUser('Oleh', 25); oleh.sayHi(); // "Hello, Oleh"

A very convenient approach, especially when you do not need prototype and inheritance.


6. Object.create()

Creates a new object with the specified prototype.

javascript
const person = { greet() { console.log(`Hello, I am ${this.name}`); } }; const user = Object.create(person); user.name = 'Oleh'; user.greet(); // "Hello, I am Oleh"

Used when you need to explicitly set the prototype chain.


7. Via JSON

You can create an object from a JSON string:

javascript
const json = '{"name": "Oleh", "age": 25}'; const user = JSON.parse(json); console.log(user.name); // "Oleh"

Convenient when receiving data from a server. But it only works with simple data types (no methods).


8. Via Object.assign()

Creates an object based on others (copies properties):

javascript
const base = { a: 1 }; const extra = { b: 2 }; const result = Object.assign({}, base, extra); console.log(result); // { a: 1, b: 2 }

Can be used for cloning or merging objects.


9. Via the spread operator { ...obj } (ES6)

javascript
const user = { name: 'Oleh', age: 25 }; const copy = { ...user, city: 'Kyiv' }; console.log(copy); // { name: 'Oleh', age: 25, city: 'Kyiv' }

The most modern way to copy and merge objects.


Additionally: Object.fromEntries()

Creates an object from an array of [key, value] pairs.

javascript
const entries = [['name', 'Oleh'], ['age', 25]]; const user = Object.fromEntries(entries); console.log(user); // { name: 'Oleh', age: 25 }

Summary

WayExampleFeatures
Object literal{ name: 'Oleh' }the most common way
new Object()new Object()rarely used
Constructor functionfunction User() { this.name = ... }an old alternative to class
classclass User { ... }a modern OOP approach
Factory functionfunction createUser() { return {...} }no this, just returns an object
Object.create(proto)Object.create(person)creates an object with a given prototype
JSON.parse()JSON.parse('{"a":1}')creates an object from a string
Object.assign()Object.assign({}, obj1, obj2)copies/merges objects
Spread operator{ ...obj1, ...obj2 }a short copying syntax
Object.fromEntries()Object.fromEntries([['a',1]])from an array of pairs into an object

In short

The most popular way -> {} For inheritance -> Object.create() For templates -> class or a constructor function For copying -> { ...obj } or Object.assign()

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.