Math Object Cheat Sheet
JavaScript is packed with powerful built-in objects, and one of the most versatile among them is the Math object. If you’ve ever found yourself needing to perform complex mathematical operations in your JavaScript code, the Math object is your best friend. In this cheat sheet, we’ll explore the wealth of functionality the Math object offers, from number constants to advanced arithmetic and calculus methods. Let’s dive in!
Number Constants: Your Mathematical Constants Companion
The Math object provides several built-in number constants that are invaluable for mathematical calculations:
- Math.PI: This constant represents the mathematical constant π (pi), approximately equal to 3.14159.
- Math.E: Euler’s constant, denoted as e, approximately equal to 2.718.
- Math.LN2: The natural logarithm of 2, approximately equal to 0.693.
Rounding Methods: Precision Control at Your Fingertips
When it comes to rounding numbers, the Math object has you covered with these essential methods:
Get Pahani Imandi’s stories in your inbox
Join Medium for free to get updates from this writer.
- Math.ceil(): Rounds a number up to the nearest integer.
- Math.floor(): Rounds a number down to the nearest integer.
- Math.round(): Rounds a number to the nearest integer. If the decimal is .5 or above, it rounds up; otherwise, it rounds down.
- Math.trunc(): Trims the decimal part of a number, leaving only the integer.
Arithmetic and Calculus Methods: Unlocking Advanced Mathematical Operations
The Math object offers a plethora of arithmetic and calculus methods to tackle even the most complex computations. Here are some commonly used ones:
- Math.pow(2, 3): Calculates the value of 2 raised to the power of 3, resulting in 8.
- Math.sqrt(16): Finds the square root of 16, resulting in 4.
- Math.cbrt(8): Computes the cube root of 8, resulting in 2.
- Math.abs(-10): Returns the absolute value of -10, resulting in 10.
- Logarithmic methods: Includes Math.log(), Math.log2(), and Math.log10().
- Math.min(9, 8, 7) and Math.max(9, 8, 7): Return the minimum and maximum values among the provided inputs, respectively.
Trigonometric Methods: Navigating the World of Trigonometry
For trigonometric calculations, the Math object provides an array of functions such as Math.sin(), Math.cos(), and Math.tan(), among others.
With this cheat sheet at your disposal, you’ll be equipped to tackle a wide range of mathematical challenges in your JavaScript projects. Whether you’re building financial calculators, geometric utilities, or scientific simulations, the Math object empowers you to handle complex computations with ease.








