How do you call a static method from a class in javascript?

@Rohith K P 's answer is the solution in short.

If you want to understand it, you need to understand what JavaScript classes are “under the hood".

This is how you would create a class prior to ES6:

// This is a constructor function that initializes new Range objects. 
// Note that it does not create or return the object. It just initializes this. 
function Range(from, to) { 
    // Store the start and end points (state) of this new range object.
    // These are noninherited properties that are unique to this object. 
    this.from = from; 
    this.to = to; 
} 

// All Range objects inherit from this object. 
// Note that the property name must be "prototype" for this to work. 
// Note that the prototype property is the property of the Range function
Range.prototype = { 
    // create some methods

    // Return true if x is in the range, false otherwise 
    // This method works for textual and Date ranges as well as numeric. 
    includes: function(x) { return this.from <= x && x <= this.to; },

    // A generator function that makes instances of the class iterable. 
    // Note that it only works for numeric ranges. 
    [Symbol.iterator]: function*() { 
        for( let x = Math.ceil( this.from); x <= this.to; x ++) yield x; 
    }, 

    // Return a string representation of the range 
    toString: function() { return "(" + this.from + "..." + this.to + ")"; } 
}; 

// Here are example uses of this new Range class 
let r = new Range(1,3); 
// r inherits from Range.prototype
r.includes(2) // = > true: 2 is in the range 
r.toString() // = > "(1...3)" 
[...r] // => [1, 2, 3]; convert to an array via iterator

Image of inheritance strructure in this case

So, a class is essentially a function, which is a public interface (constructor) of its prototype object.

let F = function() {}; // This is a function object.
let p = F.prototype; // This is the prototype object associated with F. 
let c = p.constructor; // This is the function associated with the prototype. 
c === F // true

It is important to understand that the class defined in this Example works in exactly the same way as ES6 classes. The introduction of the "class" keyword to the language does not alter the fundamental nature of JavaScript’s prototype-based classes.

Static methods

Static methods are defined as properties of the constructor function rather than properties of the prototype object.

static parse(s) { 
    let matches = s.match(/^\((\d+)\.\.\.(\d+)\)$/); 
    if (!matches) { 
        throw new TypeError(`Cannot parse Range from "${s}".`) 
    } 
    return new Range(parseInt(matches[1]), parseInt(matches[2])); 
}

The method defined by this code is Range.parse(), not Range.prototype.parse(), and you must invoke it through the constructor, not through an instance:

let r = Range.parse('(1...10)'); // Returns a new Range object 
r.parse('(1...10)'); // TypeError: r.parse is not a function 

You’ll sometimes see static methods called class methods because they are invoked using the name of the class/ constructor. When this term is used, it is to contrast class methods with the regular instance methods that are invoked on instances of the class. Because static methods are invoked on the constructor rather than on any particular instance, it almost never makes sense to use the this keyword in a static method.

Source: Flanagan, David. JavaScript: The Definitive Guide.

The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.

Note: In the context of classes, MDN Web Docs content uses the terms properties and fields interchangeably.

Try it

Syntax

static methodName() { /* … */ }
static propertyName [= value];

// Class static initialization block
static {

}

Examples

Using static members in classes

The following example demonstrates several things:

  1. How a static member (method or property) is defined on a class.
  2. That a class with a static member can be sub-classed.
  3. How a static member can and cannot be called.

class Triple {
  static customName = 'Tripler';
  static description = 'I triple any number you provide';
  static calculate(n = 1) {
    return n * 3;
  }
}

class SquaredTriple extends Triple {
  static longDescription;
  static description = 'I square the triple of any number you provide';
  static calculate(n) {
    return super.calculate(n) * super.calculate(n);
  }
}

console.log(Triple.description);            // 'I triple any number you provide'
console.log(Triple.calculate());            // 3
console.log(Triple.calculate(6));           // 18

const tp = new Triple();

console.log(SquaredTriple.calculate(3));    // 81 (not affected by parent's instantiation)
console.log(SquaredTriple.description);     // 'I square the triple of any number you provide'
console.log(SquaredTriple.longDescription); // undefined
console.log(SquaredTriple.customName);      // 'Tripler'

// This throws because calculate() is a static member, not an instance member.
console.log(tp.calculate());                // 'tp.calculate is not a function'

Calling static members from another static method

In order to call a static method or property within another static method of the same class, you can use the this keyword.

class StaticMethodCall {
  static staticProperty = 'static property';
  static staticMethod() {
    return `Static method and ${this.staticProperty} has been called`;
  }
  static anotherStaticMethod() {
    return `${this.staticMethod()} from another static method`;
  }
}
StaticMethodCall.staticMethod();
// 'Static method and static property has been called'

StaticMethodCall.anotherStaticMethod();
// 'Static method and static property has been called from another static method'

Calling static members from a class constructor and other methods

Static members are not directly accessible using the this keyword from non-static methods. You need to call them using the class name: CLASSNAME.STATIC_METHOD_NAME() / CLASSNAME.STATIC_PROPERTY_NAME or by calling the method as a property of the constructor: this.constructor.STATIC_METHOD_NAME() / this.constructor.STATIC_PROPERTY_NAME

class StaticMethodCall {
  constructor() {
    console.log(StaticMethodCall.staticProperty); // 'static property'
    console.log(this.constructor.staticProperty); // 'static property'
    console.log(StaticMethodCall.staticMethod()); // 'static method has been called.'
    console.log(this.constructor.staticMethod()); // 'static method has been called.'
  }

  static staticProperty = 'static property';
  static staticMethod() {
    return 'static method has been called.';
  }
}

Specifications

Specification
ECMAScript Language Specification
# sec-class-definitions

Browser compatibility

BCD tables only load in the browser

See also

How do you call a static method in JavaScript?

To call them we need to create the object of the class in which it is defined. The static method gets a call in two ways one using this keyword another from the constructor. Static methods cannot directly call the nonstatic method. On-static methods use instance variable state to affect their behavior.

How do you call a static method in class?

A static method can be called from either a class or object reference. We can call it Utils if foo() is a static function in Class Utils. Utils. foo() as well as Utils().

Can we call static method with object in JavaScript?

Static class methods are defined on the class itself. You cannot call a static method on an object, only on an object class.

Can we call static method from non static method JavaScript?

No, a static method cannot call a non-static method.