Constructors in JavaScript

Published on 8 Nov, 2020

A function in JavaScript that is written to initialize newly created objects is called a constructor.

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

Constructor Function vs Normal Function

Does JavaScript in its core keep any difference between a normal function and a constructor function? NO. Both are functions according to JavaScript. The difference comes on how we use the function. It is like, I have a pen. When I use it to write on a paper, the "pen" is used as a pen. If I use the "pen" to beat someone, it is used a stick. So the object is same, but it worked in different manner based on how we used it.