Class Static Method in JavaScript

Level 4167 keystrokes

Static methods are called on the class itself, not on instances of the class.

JavaScript code

// Define a utility class with static methods
1class MathUtils {
// static keyword makes this a class method
2 static add(a, b) {
// Return the sum of two numbers
3 return a + b;
4 }
// Another static method for multiplication
5 static multiply(a, b) {
// Return the product of two numbers
6 return a * b;
7 }
8}
// Call static method directly on the class
9console.log(MathUtils.add(5, 3));
// Call multiply static method
10console.log(MathUtils.multiply(4, 2));

Output

8
8
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • (× 6
  • )× 6
  • ,× 4
  • ;× 4
  • .× 4
  • {× 3
  • }× 3
  • *× 1
  • +× 1

More JavaScript examples at level 4

← JavaScript typing practice