Can JavaScript classes/objects have constructors? How are they created?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Yes, JavaScript classes and objects can have constructors, which are special methods used to initialize the state of an object when it is created. Constructors are a common feature in object-oriented programming and are supported in JavaScript through class constructors and constructor functions.

How JavaScript Constructors Work

1. Constructors in Classes

In JavaScript classes, the constructor method is a special method used to initialize objects. It is called automatically when a new instance of the class is created using the new keyword.

Syntax and Example:

class Person { constructor(name, age) { // Constructor method this.name = name; // Initialize properties this.age = age; } greet() { // Instance method console.log(`Hi, I'm ${this.name} and I'm ${this.age} years old.`); } } // Create instances using the constructor const person1 = new Person('Alice', 30); const person2 = new Person('Bob', 25); person1.greet(); // Output: Hi, I'm Alice and I'm 30 years old. person2.greet(); // Output: Hi, I'm Bob and I'm 25 years old.

Key Features of constructor in Classes:

  • A class can have only one constructor method.
  • If no constructor is explicitly defined, JavaScript provides a default constructor.
  • The constructor can accept parameters to initialize object properties.

2. Constructor Functions

Before the introduction of ES6 classes, JavaScript used constructor functions to create objects. These are regular functions called with the new keyword.

Syntax and Example:

function Person(name, age) { // Constructor function this.name = name; // Initialize properties this.age = age; this.greet = function() { // Method defined on the instance console.log(`Hi, I'm ${this.name} and I'm ${this.age} years old.`); }; } // Create instances using the constructor function const person1 = new Person('Alice', 30); const person2 = new Person('Bob', 25); person1.greet(); // Output: Hi, I'm Alice and I'm 30 years old. person2.greet(); // Output: Hi, I'm Bob and I'm 25 years old.

Key Features of Constructor Functions:

  • They rely on the new keyword to create new objects.
  • When called with new, the function sets this to the newly created object.
  • Methods defined inside the constructor are unique to each instance, which can be inefficient for memory usage.

3. Using class vs. Constructor Functions

With the introduction of ES6 classes, class syntax is now the preferred way to define constructors because:

  • It is cleaner and more readable.
  • Methods defined in classes are shared across instances via the prototype, making them more memory-efficient.

Example:

// ES6 Class (preferred) class Person { constructor(name, age) { this.name = name; this.age = age; } greet() { console.log(`Hi, I'm ${this.name} and I'm ${this.age} years old.`); } } const person = new Person('Charlie', 40); person.greet();

4. Object Literals Without Constructors

For simple objects, you can define properties directly in an object literal, without a constructor.

Example:

const person = { name: 'Alice', age: 30, greet() { console.log(`Hi, I'm ${this.name} and I'm ${this.age} years old.`); } }; person.greet(); // Output: Hi, I'm Alice and I'm 30 years old.

This approach is useful for small, static objects that don't require multiple instances.

5. Default and Parameterized Constructors in Classes

If you don’t define a constructor, JavaScript provides a default one.

Example of Default Constructor:

class Animal { sound() { console.log('Some sound'); } } const animal = new Animal(); // No parameters needed animal.sound(); // Output: Some sound

You can also create parameterized constructors:

class Animal { constructor(type) { this.type = type; } sound() { console.log(`${this.type} makes a sound.`); } } const dog = new Animal('Dog'); dog.sound(); // Output: Dog makes a sound.

Summary of How to Create Constructors

MethodExampleUse Case
Class Constructorclass Person { constructor(name) { ... } }Preferred for modern, object-oriented designs
Constructor Functionfunction Person(name) { this.name = name; }Legacy code or pre-ES6 environments
Object Literal Without Constructor{ name: 'Alice', greet() { ... } }Simple, static objects

For modern applications, ES6 classes with constructor are the recommended approach because they are more readable, efficient, and align with modern JavaScript practices. If you're keen to explore more JavaScript patterns, consider checking out Grokking Advanced Coding Patterns for Interviews or similar resources on DesignGurus.io!

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How are Python's Built In Dictionaries Implemented?
What is the most difficult thing in software development?
Hands-on practice with real-world coding interview scenarios
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.