← All material

Core JavaScript · 4 of 9

Functions Over Classes

A lot of "utility classes" made of nothing but static methods are a namespace wearing a class costume:

The old way

class MathUtils {
  static square(n: number) {
    return n * n;
  }
  static clamp(n: number, min: number, max: number) {
    return Math.min(Math.max(n, min), max);
  }
}

MathUtils.square(4);

Nothing here needs an instance, inheritance, or instanceof — it's grouping for grouping's sake. A module already groups related code; you don't need a class on top of it.

// math-utils.js

export const square = (n) => {
  return n * n;
};

export const clamp = (n, min, max) => {
  return Math.min(Math.max(n, min), max);
};



// usage
import { square } from "./math-utils.js";
square(4);

This isn't "classes are bad" — reach for one when you genuinely have many instances, need instanceof checks, or want prototype-based method sharing for performance. The test is: does anything here actually vary per instance? If not, it's functions in a module.