Constructors
A constructor is a special method that runs when you create a new instance of a class.
class User {
constructor(name) {
this.name = name;
}
}
const user = new User("Alice");
console.log(user.name); // AliceThe constructor receives "Alice" and assigns it to the new object's name property.
What a Constructor Does
A constructor usually sets up the initial state of an object.
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
}
const keyboard = new Product("Keyboard", 1200);
console.log(keyboard.name); // Keyboard
console.log(keyboard.price); // 1200When new Product(...) runs:
- JavaScript creates a new empty object.
- It sets
thisto that new object. - It runs the constructor.
- It returns the new object.
Constructor Parameters
Constructors can accept parameters like normal functions.
class User {
constructor(name, role) {
this.name = name;
this.role = role;
}
}
const admin = new User("Alice", "admin");
const editor = new User("Bob", "editor");Each instance gets its own property values.
console.log(admin.role); // admin
console.log(editor.role); // editorDefault Values
Constructors can use default parameters.
class User {
constructor(name, role = "user") {
this.name = name;
this.role = role;
}
}
const alice = new User("Alice", "admin");
const bob = new User("Bob");
console.log(alice.role); // admin
console.log(bob.role); // userDefaults are useful when most instances use the same value.
Constructor With Object Parameter
When there are many options, an object parameter is often clearer.
class User {
constructor({ name, role = "user", active = true }) {
this.name = name;
this.role = role;
this.active = active;
}
}
const user = new User({
name: "Alice",
role: "admin",
});This avoids confusing positional arguments.
Compare:
new User("Alice", "admin", true);With:
new User({ name: "Alice", role: "admin", active: true });The object version is easier to read.
Validating Constructor Input
Constructors can validate data.
class Product {
constructor(name, price) {
if (price < 0) {
throw new Error("Price cannot be negative");
}
this.name = name;
this.price = price;
}
}This prevents invalid instances.
const product = new Product("Keyboard", -100); // ErrorUse validation when an object should never exist in an invalid state.
Constructors Do Not Need to Return
Constructors usually do not return a value.
class User {
constructor(name) {
this.name = name;
}
}JavaScript returns the new instance automatically.
Avoid returning custom values from constructors.
It is confusing and rarely needed.
No Constructor Needed
If a class does not need setup, you can omit the constructor.
class Logger {
log(message) {
console.log(`[LOG] ${message}`);
}
}
const logger = new Logger();
logger.log("App started");JavaScript provides a default constructor automatically.
Instance Properties
Properties assigned with this become instance properties.
class Counter {
constructor() {
this.count = 0;
}
}
const first = new Counter();
const second = new Counter();
first.count = 5;
console.log(first.count); // 5
console.log(second.count); // 0Each instance has its own count.
Constructor and this
Inside a constructor, this refers to the new instance being created.
class User {
constructor(name) {
console.log(this);
this.name = name;
}
}
new User("Alice");You learned earlier that this depends on how a function is called.
With classes, new creates a new object and sets this to that object.
Best Practices
Use constructors to initialize required instance data.
Use default parameters for common defaults.
Use an options object when there are many parameters.
Validate input when invalid objects should not exist.
Avoid doing heavy work in constructors.
Keep constructors easy to understand.
Common Mistakes
Mistake 1: Forgetting to Assign to this
class User {
constructor(name) {
name = name;
}
}
const user = new User("Alice");
console.log(user.name); // undefinedCorrect:
this.name = name;Mistake 2: Putting Too Much Logic in the Constructor
Constructors should set up the object.
Avoid making network requests, starting timers, or doing complex work directly inside constructors unless there is a clear reason.
Mistake 3: Relying on Argument Order Too Much
new User("Alice", true, "admin", false);This is hard to read.
Use an options object for many settings.
Quick Check
What does this log?
class User {
constructor(name, role = "user") {
this.name = name;
this.role = role;
}
}
const user = new User("Alice");
console.log(user.role);It logs:
userSummary
Constructors initialize new class instances.
constructorruns whennew ClassName(...)is used.- Inside a constructor,
thisis the new instance. - Constructor parameters can set instance properties.
- Defaults and options objects make constructors more flexible.
- Constructors usually do not return values.
- Use validation to prevent invalid instances.
- Omit the constructor when no setup is needed.