Lesson 10: JavaScript Math Object
Master mathematical operations in JavaScript with the powerful Math object and interactive examples.
10.1 Introduction to the Math Object
The JavaScript Math object provides properties and methods for mathematical constants and functions. It allows you to perform mathematical tasks on numbers.
Key features: The Math object allows you to perform common mathematical operations like rounding, trigonometry, exponentiation, logarithms, and more. It also provides mathematical constants like π and e.
Example 10.1: Using Math Methods
// Absolute value const absValue = Math.abs(-7.25); // Square root const sqrtValue = Math.sqrt(64); // Power function const powValue = Math.pow(4, 3); // Rounding functions const roundValue = Math.round(4.7); const ceilValue = Math.ceil(4.4); const floorValue = Math.floor(4.7); // Trigonometric functions const sinValue = Math.sin(Math.PI / 2); const cosValue = Math.cos(Math.PI); document.write(`Absolute value of -7.25: ${absValue}`); document.write(`<br>Square root of 64: ${sqrtValue}`); document.write(`<br>4 raised to power 3: ${powValue}`); document.write(`<br>Round 4.7: ${roundValue}`); document.write(`<br>Ceil 4.4: ${ceilValue}`); document.write(`<br>Floor 4.7: ${floorValue}`); document.write(`<br>Sine of π/2: ${sinValue}`); document.write(`<br>Cosine of π: ${cosValue}`);
10.2 Math Methods
The Math object provides numerous methods to perform mathematical operations. Here are some of the most commonly used ones:
Rounding Methods
Results will appear here
Powers & Roots
Results will appear here
Trigonometry
Results will appear here
Min/Max/Random
Results will appear here
Method | Description | Example |
---|---|---|
abs(x) | Returns absolute value of x | Math.abs(-4.7) → 4.7 |
ceil(x) | Rounds x up to nearest integer | Math.ceil(4.4) → 5 |
floor(x) | Rounds x down to nearest integer | Math.floor(4.7) → 4 |
round(x) | Rounds x to nearest integer | Math.round(4.7) → 5 |
max(x,y,z,...n) | Returns highest-valued number | Math.max(0, 150, 30, 20) → 150 |
min(x,y,z,...n) | Returns lowest-valued number | Math.min(0, 150, 30, 20) → 0 |
pow(x,y) | Returns x to the power of y | Math.pow(8, 2) → 64 |
sqrt(x) | Returns square root of x | Math.sqrt(64) → 8 |
random() | Returns random number between 0 and 1 | Math.random() → 0.123456 |
sin(x) | Returns sine of x (x in radians) | Math.sin(Math.PI/2) → 1 |
cos(x) | Returns cosine of x (x in radians) | Math.cos(Math.PI) → -1 |
tan(x) | Returns tangent of x (x in radians) | Math.tan(Math.PI/4) → 1 |
10.3 Math Properties
The Math object also provides several mathematical constants as read-only properties:
Math.PI
3.141592653589793
Ratio of circumference to diameter
Math.E
2.718281828459045
Euler's constant
Math.SQRT2
1.4142135623730951
Square root of 2
Math.SQRT1_2
0.7071067811865476
Square root of 1/2
Math.LN2
0.6931471805599453
Natural log of 2
Math.LN10
2.302585092994046
Natural log of 10
Math.LOG2E
1.4426950408889634
Base 2 log of E
Math.LOG10E
0.4342944819032518
Base 10 log of E
Property | Description | Value (approx.) |
---|---|---|
Math.PI | Pi, the ratio of circle circumference to diameter | 3.141592653589793 |
Math.E | Euler's constant, base of natural logarithms | 2.718281828459045 |
Math.SQRT2 | Square root of 2 | 1.4142135623730951 |
Math.SQRT1_2 | Square root of 1/2 | 0.7071067811865476 |
Math.LN2 | Natural logarithm of 2 | 0.6931471805599453 |
Math.LN10 | Natural logarithm of 10 | 2.302585092994046 |
Math.LOG2E | Base 2 logarithm of E | 1.4426950408889634 |
Math.LOG10E | Base 10 logarithm of E | 0.4342944819032518 |
10.4 Real-World Applications
Math object methods are essential for many real-world applications:
10.4.1: Circle Calculator
Results will appear here
10.4.2: Random Password Generator
Your password will appear here
10.4.3: Distance Calculator
Distance will appear here