Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Static methods belong to the class itself, not to instances. They're like utility functions that live inside a class - call them directly on the class, no instance needed!
class Calculator {
add(a, b) {
return a + b;
}
}
const calc = new Calculator();
calc.add(5, 3); // 8Need to create instance first
class Calculator {
static add(a, b) {
return a + b;
}
}
Calculator.add(5, 3); // 8Call directly on class!
Call methods without creating instances
Math operations, string formatting, data conversion
Create instances in specific ways
Check data before creating instances
Class-level settings and constants
Common use cases
Math.round(4.7); // 5 Math.floor(4.7); // 4 Math.random(); // 0.123... Math.max(1, 5, 3); // 5
Object.keys({a: 1, b: 2}); // ['a', 'b']
Object.values({a: 1, b: 2}); // [1, 2]
Object.entries({a: 1, b: 2}); // [['a',1],['b',2]]Array.isArray([1, 2]); // true
Array.from('hello'); // ['h','e','l','l','o']
Array.of(1, 2, 3); // [1, 2, 3]JavaScript's native static methods
thisSide-by-side comparison
Database connection class with static methods
StringHelper.capitalize()User.isValidEmail()Color.fromHex()Database.configure()IDGenerator.generate()Math.round()), instance methods are called on objects (e.g., array.push())!