JavaScript Interview Question #27

How can you create objects in JavaScript?

Objects, Arrays & Core Data Handling Junior Beginner

Quick Interview Answer

The most common approach is an object literal using braces. Objects can also be created with constructor functions, classes, Object.create, or the Object constructor.

Detailed Explanation

The most common approach is an object literal using braces. Objects can also be created with constructor functions, classes, Object.create, or the Object constructor.

Object literals are normally preferred for simple objects because they are concise. Classes or factory functions are useful when many objects should share consistent construction behavior.

Code Example

const product = {
    id: 10,
    name: "Laptop"
};

const empty = new Object();

const baseUser = { role: "User" };
const user = Object.create(baseUser);
user.name = "Asif";