The main differences: syntax: class syntax is cleaner, keeping everything together under a single class block and handling a lot of the boilerplate for you. safety: Code run in class definitions is always in strict mode. You also can't accidentally call a class constructor as a function or an error will be thrown. built-ins support/initialization: class initialization starts at the base class and works down to the derived class. This allows base classes to define the this (the new instance) which in turn allows class syntax to correctly support extending built-ins like Array. Alternatively, function constructors create the instance in the derived class and pass it up to the base for initialization so a base class has no influence over what this is. feature support: class syntax can support features that function constructors don't, in particular private members . The upcoming support for decorators is also only for class syntax. Some other smaller differences: When using toString() class functions will always use the class syntax (though shouldn't be too surprising since toString() defaults to showing the source of the function) The prototype on a class-defined constructor is defined as read-only. whereas function prototype properties are not function declaration definitions are hoisted, class declaration definitions are not Being in a class block allows a local name binding for the class (assuming its named) to be available to all members defined within the class block Answer from senocular on reddit.com
🌐
Reddit
reddit.com › r/learnjavascript › what is the difference between "class" and "constructor function" in js? i tried looking up on yt but found nothing relevant atlest for me.
r/learnjavascript on Reddit: What is the difference between "class" and "constructor function" in js? I tried looking up on YT but found nothing relevant atlest for me.
May 16, 2022 -

So I am learning NodeJs and while creating a costum module which stores user data I created a constructor function in user.js

function User(name,age){
this.name = name;
this.age = age;
}

and in app.js I did

const User = require(//Path);

const user1 = new User("Adam",20);
console.log(user1.name);

but my consfusion begans when I used class in user.js like

class User{
    constructor(name,age){
        this.name = name;
        this.age = age;
    }
}

but the code in the app.js remained similar with no errors.

So what is the point of classes in javasript? And how are they different form contructor functions.

Top answer
1 of 7
15
The main differences: syntax: class syntax is cleaner, keeping everything together under a single class block and handling a lot of the boilerplate for you. safety: Code run in class definitions is always in strict mode. You also can't accidentally call a class constructor as a function or an error will be thrown. built-ins support/initialization: class initialization starts at the base class and works down to the derived class. This allows base classes to define the this (the new instance) which in turn allows class syntax to correctly support extending built-ins like Array. Alternatively, function constructors create the instance in the derived class and pass it up to the base for initialization so a base class has no influence over what this is. feature support: class syntax can support features that function constructors don't, in particular private members . The upcoming support for decorators is also only for class syntax. Some other smaller differences: When using toString() class functions will always use the class syntax (though shouldn't be too surprising since toString() defaults to showing the source of the function) The prototype on a class-defined constructor is defined as read-only. whereas function prototype properties are not function declaration definitions are hoisted, class declaration definitions are not Being in a class block allows a local name binding for the class (assuming its named) to be available to all members defined within the class block
2 of 7
15
In most practical situations, there is little to no difference. Classes in Javascript are what is called syntactic sugar which means it's just a different way or representing the same thing.
Top answer
1 of 2
4

A class is a constructor technically. Any object with a [[ Construct ]] internal method is considered one. All constructors are functions, but not all functions are constructors.

Functions in Javascript can be of many kinds. We have normal functions, arrow functions, class, methods, async functions, async arrow functions, generators, async generators and possibly in the future generator arrow functions.

Out of all these functions, the only ones who can construct something are class and normal functions. None other of these have a [[ Construct ]] internal method.

There are some semantic differences between normal functions and classes as constructors and as callable functions:

  • Only classes have derived constructors (which have a slightly different construction model).
  • Classes can't be called, only instantiated through new.
  • Classes have a super binding (which part of it is related to 1).
  • The prototype of the class (not the prototype property) is the super constructor when there's an extends clause.
  • The prototype property is not writable in classes, it is writable for normal functions.

Some other things that might be different from class but can be done with normal functions:

  • class code is strict mode by default.
  • class uses methods in their prototype. For normal functions as constructors they are typically also regular functions. This mean you can't construct new objects from class methods.
  • class methods in the prototype are not enumerable. For normal functions as constructors they are typically defined with = and those make the properties enumerable.

Other relevant information:

  • Classes are not instantiated until they reach the class declaration/expression. Top level function declarations are initialized right at the start of the function call and are accessible through out the whole scope.
  • constructor(){ } in class is the only method definition that actually evaluates to a constructor. Methods are not constructors.
2 of 2
2

What @Keith said is quite true, and it is, for some odd reason, very hard for JS new-comers to grasp, consider this code:

Old School

With the old school way, things get syntactically messy:

function _Thing() {}

function _Person(name, age, from) {
    this.name = name;
    this.age = age;
    this.from = from;
}

_Person.prototype = Object.create(_Thing.prototype);
_Person.prototype.constructor = _Person;//so easy to mess this up!

_Person.prototype.makeOlder = function () {
    this.age++;
};
_Person.prototype.toString = function () {
    return this.name;
};
_Person.FLORIDA = 1;
_Person.NEW_YORK = 2;


var _p = new _Person('rafael cepeda', 23, _Person.FLORIDA);
console.log(_p, _p instanceof _Person, _p instanceof _Thing);//_Person { name: 'rafael cepeda', age: 23, from: 1 } true true

New School

The ES6 way provides a very intuitive feeling for OOP programmers:

class Thing {}

class Person extends Thing {
    constructor(name, age, from) {
        super();
        this.name = name;
        this.age = age;
        this.from = from;
    }

    makeOlder() {
        this.age++;
    }

    toString() {
        return this.name;
    }

    static get FLORIDA() { return 1; }
    static get NEW_YORK() { return 2; }
}

var p = new Person('rafael cepeda', 23, Person.FLORIDA);
console.log(p, p instanceof Person, p instanceof Thing);//Person { name: 'rafael cepeda', age: 23, from: 1 } true true

Notice the parallels between the ES6 style constructor and the old school constructor, take a look at the prototype method definitions; with the new way, you don't even have to write the word prototype (which scares js new-comers).

Because they are essentially the same thing, it is correctly coined syntactic sugar. After inspection of both constructors, it should be no surprise why both typeof's return function.

Discussions

javascript - Clarification need in class vs constructor function vs factory function - Stack Overflow
I'm playing around with ES6 classes and my eventual goal is to understand the difference between classes vs constructor functions vs factory functions. My current understanding is that constructor More on stackoverflow.com
🌐 stackoverflow.com
[Javascript] When to use classes vs constructor functions?
If you are able to use ES6+ or use Babel, then write actual classes. Unless you want to directly write in ES5, there's no reason to not use a class. More on reddit.com
🌐 r/learnprogramming
4
4
August 9, 2019
What's the difference between JS constructor functions and classes?
When you do class PersonClass{ constructor(fn,sn){ this.firstName = fn; this.surname = sn; } } let p1 = new PersonClass("Bob","Smith"); It's almost entirely equivalent to doing function PersonConstructor(fn,sn){ this.firstName = fn; this.surname = sn; } let p2 = new PersonConstructor("Jill","Jones"); We can treat them both in the same ways: PersonClass.prototype.fullName = function() { return this.firstName + " " + this.surname; } PersonConstructor.prototype.fullName = function() { return this.firstName + " " + this.surname; } console.log(p1.fullName()); console.log(p2.fullName()); Classes have added some different semantics to constructor functions. For example, while it was always possible to create private members using constructor functions, there is now direct language support for them in classes. Also, you can call a constructor function just as a function, whereas if you tried to do this with the constructor function of a class you'd get an error, but the differences are rather niche IMO. The bottom line is that, to a very large extent, classes and constructor functions produce the same underlying object/function structures; that is, they are mostly just syntactic sugar over existing functionality. More on reddit.com
🌐 r/learnjavascript
2
2
December 19, 2021
What is the difference between "class" and "constructor function" in js? I tried looking up on YT but found nothing relevant atlest for me.
The main differences: syntax: class syntax is cleaner, keeping everything together under a single class block and handling a lot of the boilerplate for you. safety: Code run in class definitions is always in strict mode. You also can't accidentally call a class constructor as a function or an error will be thrown. built-ins support/initialization: class initialization starts at the base class and works down to the derived class. This allows base classes to define the this (the new instance) which in turn allows class syntax to correctly support extending built-ins like Array. Alternatively, function constructors create the instance in the derived class and pass it up to the base for initialization so a base class has no influence over what this is. feature support: class syntax can support features that function constructors don't, in particular private members . The upcoming support for decorators is also only for class syntax. Some other smaller differences: When using toString() class functions will always use the class syntax (though shouldn't be too surprising since toString() defaults to showing the source of the function) The prototype on a class-defined constructor is defined as read-only. whereas function prototype properties are not function declaration definitions are hoisted, class declaration definitions are not Being in a class block allows a local name binding for the class (assuming its named) to be available to all members defined within the class block More on reddit.com
🌐 r/learnjavascript
20
31
May 16, 2022
🌐
Medium
medium.com › @hridoymahmud › understanding-javascript-constructors-function-vs-class-63d140a44288
Understanding JavaScript Constructors: Function vs. Class | by Mahmudur Rahman | Medium
July 11, 2024 - Refactoring the entire codebase to use ES6 classes might introduce unnecessary complexity and bugs. In such cases, sticking with constructor functions is a practical choice. Class constructors are ideal for new projects or when you want to take advantage of modern JavaScript features.
🌐
Codecademy
codecademy.com › forum_questions › 54dcaa67d3292f11f1001277
The difference between class , object and constructor ? | Codecademy
So you could think of the constructor as “the class” but not really, in other languages every class has a constructor to instantiate objects, in JavaScript, the “class” and the constructor are the same thing.
🌐
DEV Community
dev.to › gnio › eli5-functions-vs-class-constructor-in-javascript-nki
ELI5: Functions vs. Class/Constructor in Javascript - DEV Community
June 14, 2020 - In this example, person_a and person_b are instances of the blueprint (read: class) Person. Each of them gets its own first_name and last_name instance variables, and print_full_name method. Now JavaScript, while seemingly does the same thing, does this differently. Whenever we use the new keyword to execute a function, we create an implicit object that we can reference with this inside the function. Furthermore, objects created with a constructor function have a reference to the constructor function inside their own prototypes.
🌐
DEV Community
dev.to › ayako_yk › javascript-constructors-vs-classes-4o8i
JavaScript Constructors vs. Classes - DEV Community
March 19, 2025 - So, essentially, constructor functions and classes do the same things under the hood. However, the newly introduced class syntax adds features such as: ... In the upcoming blogs, I'll continue exploring classes in JavaScript.
Find elsewhere
🌐
Talent500
talent500.com › blog › javascript-constructors-comprehensive-guide
JavaScript Constructors: A Comprehensive Guide for Developers
January 29, 2025 - ES6 classes introduce several new features that were difficult or impossible to implement with traditional constructor functions: Super calls: The super keyword allows for easier and more intuitive supercalls to parent class methods. Static methods: Classes support static methods, which are called on the class itself rather than on instances. Private fields and methods: ES6 classes support private members, improving encapsulation and data hiding. If someone novice to JavaScript started to learn this, if they have worked with any object-oriented programming languages it makes it easier for them to understand and work with JavaScript’s object model.
Top answer
1 of 3
7

Since it returns an object its a factory function - it's already explained there.

Constuctor functions behaviour different from this, it doesn't return value:

function Person(name, age, location, occupation){
    this.name = name
    this.age = age
    this.location = location
    this.occupation = occupation
}

Person.prototype.printDetails = function(){
        console.log(`My name is ${this.name} and I'm ${this.age} years old. I live in ${this.location} and I work as a ${this.occupation}.`);
};

const secondUser = new Person('Johnny', 25, 'London', 'Driver');
secondUser.printDetails();

I used definition of methods by extending prototype just to separate constructor function, and you can still define a methods inside constructor function as well:

function Person(name, age, location, occupation){
    this.name = name
    this.age = age
    this.location = location
    this.occupation = occupation

    this.printDetails = function(){
        console.log(`My name is ${this.name} and I'm ${this.age} years old. I live in ${this.location} and I work as a ${this.occupation}.`);
    };
}

const secondUser = new Person('Johnny', 25, 'London', 'Driver');
secondUser.printDetails();
2 of 3
4

Javascript takes its roots in Prototypal Inheritance:

// Prototype
var ProtoCtr = function ProtoCtr(name, age) {
    this.name = name;
    this.age = age;
};
ProtoCtr.prototype.printDetails = function printDetails() { console.log(`Hi, I'm ${this.name} and I'm ${this.age} years old`); };

var protoInstance = new ProtoCtr('John', 21);
protoInstance.printDetails();

In short:

  • Functions expose prototypes

  • Defines a this context referring to the instance

  • Use the new keyword to create an instance

Then, with ES6, the language offered the possibility to use the class keyword (to please classical OO developers). It's a syntax which allow to group the declaration of your object in a block, avoiding to extend the prototype "manually". Under the hood, it does the exact same thing than the prototypal model, it's just an other way to write it.

// Class
class ClassCtr {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    printDetails() {
        console.log(`Hi, I'm ${this.name} and I'm ${this.age} years old`);
    }
}
var classInstance = new ClassCtr('John', 21);
classInstance.printDetails();

Key concepts are about the same, but you don't use prototype to expose methods to your instance but declare it directly inside the class block.

Then, there is the factory pattern, which is not a language specs but a pattern, a way to do things. It's my personal favorite. The factory function builds and return the instance. With this method, you get rid of the new keyword and don't need the this anymore to refer to you instance:

// Factory
var factoryCtr = function factoryCtr(name, age) {
    var instance = {
        name: name,
        age: age,
        printDetails: function printDetails() { console.log(`Hi, I'm ${instance.name} and I'm ${instance.age} years old`); }
    };
    return instance;
}
var factoryInstance = factoryCtr('John', 21);
factoryInstance.printDetails();

Even if Class is not covered by this talk, I recommand you to watch this video: https://www.youtube.com/watch?v=ya4UHuXNygM

I hope this helped :)

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Classes › constructor
constructor - JavaScript - MDN Web Docs - Mozilla
The constructor must be a literal name. Computed properties cannot become constructors. ... class Foo { // This is a computed property. It will not be picked up as a constructor.
🌐
DEV Community
dev.to › adrianmarkperea › of-classes-and-constructor-functions-how-javascript-is-different-from-other-oop-languages-4l35
Of Classes and Constructor Functions: How JavaScript is Different from Other OOP Languages - DEV Community
June 18, 2020 - These constructor functions are the JavaScript analog of the class. Now while it seems that this is the same thing as the other languages we've mentioned, JavaScript behaves differently whenever we use these constructor functions.
🌐
Codecademy
codecademy.com › forum_questions › 5259d99780ff336c0e00656d
What's the difference between class and constructor? | Codecademy
constructor is a that part or portion of a class defination, which mainly consists of properties/method definations using “this” keyword. ... The constructor is an object that delegates properties to new instances. this is that new instance ...
🌐
GitConnected
levelup.gitconnected.com › constructor-functions-vs-class-definitions-in-javascript-who-wins-1ec08efb2591
Constructor Functions vs. Class Definitions in JavaScript — Who WINS? | by Ebo Jackson | Level Up Coding
November 17, 2024 - Backward Compatibility: Constructor functions are supported in all versions of JavaScript, making them a reliable choice for older environments. Flexibility: Prototypes allow for adding and modifying methods and properties dynamically. Memory Efficiency: Methods defined on the prototype are shared across instances, reducing memory usage. ... Syntax Complexity: The syntax for constructor functions and prototypes can be cumbersome and less intuitive compared to class syntax.
🌐
Reddit
reddit.com › r/learnprogramming › [javascript] when to use classes vs constructor functions?
r/learnprogramming on Reddit: [Javascript] When to use classes vs constructor functions?
August 9, 2019 -

I know that classes in Javascript are really just "syntactic sugar", as they say, over the prototype model. I come from more of object oriented background and am trying to determine what the difference is/pros and cons of classes vs constructor functions. They seem essentially the same to me, with the exception of the ability to use scope and closures in constructor functions to create "private variables". When should one use classes vs constructor functions? Are they interchangeable in most situations?

🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Using_classes
Using classes - JavaScript - MDN Web Docs - Mozilla
January 23, 2026 - In JavaScript, classes are mainly ... are normal JavaScript values as well, and have their own prototype chains. In fact, most plain JavaScript functions can be used as constructors — you use the new operator with a constructor function to create a new object...
🌐
Reddit
reddit.com › r/learnjavascript › what's the difference between js constructor functions and classes?
r/learnjavascript on Reddit: What's the difference between JS constructor functions and classes?
December 19, 2021 -

So I was reading this article: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS

And then I remember that JS also has classes. Now i'm confused what's the difference between the two. I tried searching about JS classes and it turns out they're introduced from ES6. They're defined as template for javascript objects. How is that any different from constructor functions which according to the article above:

The constructor function is JavaScript's version of a class.

🌐
Medium
medium.com › @AnudeepthiKolagani › ℂ-ℂ-5cfaaa09b224
Constructor Functions vs Classes in JavaScript | Medium
July 14, 2025 - The main difference between a method and a constructor method is A method contains some business logic to process the object data and a constructor method contains object initialization logic.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › js-constructor-method
JavaScript Constructor Method - GeeksforGeeks
A constructor is a special function used to create and initialize objects, defined using the function keyword or class syntax. The new keyword triggers the constructor, creating a new object and setting this to refer to it.
Published   2 weeks ago
🌐
Quora
quora.com › Did-JavaScript-really-need-classes-when-it-had-object-constructors
Did JavaScript really need classes when it had object constructors? - Quora
Use constructor/factory functions when: you need per-instance private state via closures; require unusual prototype shapes or multiple dispatch-like patterns; want minimal transpilation/legacy-footprint. Conclusion Classes are primarily ergonomic and correctness-oriented sugar over JavaScript’s existing prototype system.
🌐
LinkedIn
linkedin.com › pulse › class-object-constructor-function-javascript-amendra-botejue
Class and Object Constructor Function in JavaScript
April 17, 2023 - Classes and object constructor ... While classes provide a more convenient syntax for defining objects, both classes and constructor functions can be used to create objects with properties and methods....