JavaScript provides multiple ways to create objects.
Below are the 7 most important ones for interviews.
✅ 1. Object Literal (Most Common)
const user = {
name: "Teekam",
age: 26
};
✔ Easiest & most readable
✔ Best for simple objects
✅ 2. Using new Object() Constructor
const user = new Object();
user.name = "Teekam";
user.age = 26;
✔ Rarely used
✔ Same as object literal but longer
✅ 3. Using a Constructor Function
Before ES6, this was the main method for object creation.
function User(name, age) {
this.name = name;
this.age = age;
}
const u1 = new User("Teekam", 26);
✔ Allows creation of multiple similar objects
✔ Supports prototype inheritance
✅ 4. Using ES6 Classes
Classes are syntactic sugar over constructor functions.
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const u1 = new User("Teekam", 26);
✔ Cleaner, modern
✔ Supports inheritance
✔ Most used in React + Node.js
✅ 5. Using Object.create()
Creates an object with a specific prototype.
const proto = {
greet() {
console.log("Hello");
}
};
const user = Object.create(proto);
user.name = "Teekam";
user.greet(); // Hello
✔ Best for prototype-based inheritance
✔ More flexible than classes
✅ 6. Using Factory Function
A normal function that returns an object.
function createUser(name, age) {
return {
name,
age,
greet() {
console.log(`Hi, I am ${name}`);
}
};
}
const u1 = createUser("Teekam", 26);
✔ No need for new
✔ Avoids this confusion
✔ Very popular in real codebases
✅ 7. Using JSON
Sometimes we parse JSON to create objects.
const obj = JSON.parse('{"name": "Teekam", "age": 26}');
✔ Useful when working with APIs
✔ Converts JSON → object
⭐ Bonus: Object.assign() (Clone or Merge)
const obj = Object.assign({}, { a: 1, b: 2 });
✔ Creates a new object by copying properties
✔ Used for shallow cloning
🎯 Short Interview Answer
You can create objects in JavaScript using object literals,
new Object(), constructor functions, ES6 classes,Object.create(), factory functions, and JSON parsing.
The most common way is using object literals, while classes and factory functions are commonly used in modern applications.