Another method other than a constructor in a JavaScript class?

JavascriptObject Oriented ProgrammingFront End Technology

The constructor() method is special. It is where we initialize properties. It is called automatically when a class is initiated. In fact, if we do not have a constructor() method, JavaScript will add an invisible and empty constructor() method. We are also free to make our own methods. The creation of our own method follows the same syntax as the original syntax.


In the following example, rather than using the default method constructor() the properties were actually initialized in a user given method called "anotherMet()". Through this method, the actual result is executed in the output as shown.

Live Demo

<p id="method"></p>
   class Company {
      constructor(branch) { = branch;
      anotherMet(x) {
         return x + " is the head of " +;
   myComp = new Company("Tesla");
   document.getElementById("method").innerHTML = myComp.anotherMet("Elon musk");


Elon musk is the head of Tesla
Updated on 02-Jul-2020 12:09:06