Creating objects
An object in JavaScript is a collection of named values called properties, and when a property holds a function it is called a method. You create objects whenever you need to group related data, such as a user with a name, email and role. JavaScript offers several ways to create them, each suited to a different situation.
Object literals: the everyday choice
The simplest and most common way is an object literal: curly braces with comma-separated key-value pairs. Modern syntax adds handy shortcuts. If a variable has the same name as the key, you can write it once. Methods can skip the function keyword. Keys can even be computed from an expression using square brackets.
const name = "Ravi";
const field = "city";
const user = {
name, // same as name: name
age: 27,
[field]: "Jaipur", // computed key: city
greet() { // method shorthand
return `Hi, I am ${this.name}`;
},
};
console.log(user.greet()); // "Hi, I am Ravi"
console.log(user["city"]); // "Jaipur"
Classes and constructor functions for many similar objects
When you need many objects with the same shape, writing literals by hand repeats code. A class acts as a blueprint. Calling it with new creates a fresh object, runs the constructor to set its properties, and links it to shared methods.
class Product {
constructor(title, price) {
this.title = title;
this.price = price;
}
withTax(rate) {
return this.price * (1 + rate);
}
}
const lamp = new Product("Desk lamp", 900);
console.log(lamp.withTax(0.18)); // 1062
// Older style that does the same job
function Book(title) {
this.title = title;
}
const novel = new Book("Godaan");
The method withTax is stored once on Product.prototype and shared by every product, instead of being copied into each one. Classes are mostly a clearer syntax over the older constructor-function pattern shown at the end.
Object.create and factory functions
Object.create(proto) makes a new object whose prototype is the object you pass, which is useful for setting up inheritance by hand. Object.create(null) creates an object with no prototype at all, handy as a clean dictionary with no inherited keys.
A factory function is just a normal function that returns an object literal: const makePoint = (x, y) => ({ x, y });. It needs no new and no this, which many developers find simpler.
Mistakes that trip people up
Forgetting new with a constructor function is a classic bug. Without it, this does not refer to a new object, so properties end up on the global object (or an error is thrown in strict mode) and the function returns undefined. Classes protect you here: calling a class without new throws a clear error.
Another common slip is using an arrow function as a method in a literal. Arrow functions do not get their own this, so greet: () => this.name will not see the object's name. Use method shorthand for methods that rely on this.
Finally, remember that objects are assigned by reference. const copy = user does not create a new object; both names point at the same one. Use the spread syntax { ...user } for a shallow copy, or structuredClone(user) for a deep copy of plain data.
Which one to choose
- One-off data or configuration: object literal.
- Many objects with shared behaviour: class.
- Simple objects without
this: factory function. - Custom prototype chains or prototype-free dictionaries:
Object.create.
Takeaway: reach for literals first, use classes when you need many similar objects with methods, always call constructors with new, and copy objects explicitly because assignment only copies the reference.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.