In this chapter, we will learn about static methods in JavaScript. Static methods are functions that are defined on the class itself, rather than on instances of the class. We will cover:
- Introduction to Static Methods
- Creating Static Methods
- Using Static Methods
- Examples of Static Methods
Introduction to Static Methods
Static methods are defined on the class itself and can be called without creating an instance of the class. They are often used to create utility functions that are related to the class but do not require access to instance-specific data.
Creating Static Methods
To create a static method, you use the static
keyword followed by the method name inside a class definition.
Syntax
class ClassName { static methodName() { // method logic } }
Example
class MathUtil { static add(a, b) { return a + b; } } console.log(MathUtil.add(5, 3)); // Output: 8
In this example, the add
method is a static method of the MathUtil
class. It can be called directly on the class without creating an instance.
Using Static Methods
Static methods are called directly on the class and not on instances of the class. This makes them suitable for utility functions that do not depend on instance data.
Example: Static Method for Converting Celsius to Fahrenheit
class TemperatureConverter { static celsiusToFahrenheit(celsius) { return (celsius * 9/5) + 32; } } console.log(TemperatureConverter.celsiusToFahrenheit(0)); // Output: 32 console.log(TemperatureConverter.celsiusToFahrenheit(100)); // Output: 212
Example: Static Method for Calculating Area of a Circle
class Geometry { static areaOfCircle(radius) { return Math.PI * radius * radius; } } console.log(Geometry.areaOfCircle(5)); // Output: 78.53981633974483 console.log(Geometry.areaOfCircle(10)); // Output: 314.1592653589793
Examples of Static Methods
Example 1: Utility Class with Multiple Static Methods
class Utils { static isEven(number) { return number % 2 === 0; } static isOdd(number) { return number % 2 !== 0; } static max(...numbers) { return Math.max(...numbers); } } console.log(Utils.isEven(4)); // Output: true console.log(Utils.isOdd(3)); // Output: true console.log(Utils.max(1, 2, 3, 4, 5)); // Output: 5
Example 2: Static Method for Generating Random Numbers
class RandomUtil { static randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } } console.log(RandomUtil.randomInt(1, 10)); // Output: random integer between 1 and 10 console.log(RandomUtil.randomInt(100, 200)); // Output: random integer between 100 and 200
Example 3: Static Method for String Manipulation
class StringUtil { static capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); } } console.log(StringUtil.capitalize("hello")); // Output: Hello console.log(StringUtil.capitalize("world")); // Output: World
Conclusion
In this chapter, you learned about static methods in JavaScript, how to create them, and how to use them. Static methods are useful for creating utility functions that are related to a class but do not depend on instance-specific data. Understanding static methods is essential for writing clean and efficient JavaScript code.