Phase 6 — Numbers
JavaScript Numbers & Math Object
Numbers are one of the most commonly used data types in JavaScript. Whether you're building a shopping cart, calculator, dashboard, game, or analytics application, you'll frequently work with numbers.
JavaScript provides:
- Number methods for formatting and conversion.
- Math object for mathematical calculations.
- Random number generation.
- Rounding functions.
- Parsing functions.
Math Object
The Math object provides built-in mathematical constants and functions.
1console.log(Math.PI);
Output
13.141592653589793
Math.random()
Generates a random decimal number between 0 (inclusive) and 1 (exclusive).
1console.log(Math.random());
Example Output
10.548329
Random Integer (1–10)
1const random = Math.floor(Math.random() * 10) + 1; 2 3console.log(random);
Math.round()
Rounds to the nearest integer.
1console.log(Math.round(4.4)); 2console.log(Math.round(4.6));
Output
14 25
Math.floor()
Rounds down to the nearest integer.
1console.log(Math.floor(4.9));
Output
14
Math.ceil()
Rounds up to the nearest integer.
1console.log(Math.ceil(4.1));
Output
15
Math.trunc()
Removes the decimal part.
1console.log(Math.trunc(7.89));
Output
17
Math.max()
Returns the largest number.
1console.log(Math.max(10, 50, 20));
Output
150
Math.min()
Returns the smallest number.
1console.log(Math.min(10, 50, 20));
Output
110
Math.abs()
Returns the absolute (positive) value.
1console.log(Math.abs(-25));
Output
125
Math.pow()
Raises a number to a power.
1console.log(Math.pow(2, 3));
Output
18
Modern JavaScript also supports:
1console.log(2 ** 3);
Math.sqrt()
Returns the square root.
1console.log(Math.sqrt(64));
Output
18
Math.sign()
Returns the sign of a number.
1console.log(Math.sign(-20)); 2console.log(Math.sign(0)); 3console.log(Math.sign(50));
Output
1-1 20 31
Math.PI
Represents the value of π.
1const radius = 5; 2 3const area = Math.PI * radius * radius; 4 5console.log(area);
Number.toFixed()
Formats a number with fixed decimal places.
1const price = 123.4567; 2 3console.log(price.toFixed(2));
Output
1123.46
Number.toPrecision()
Formats a number using a specified precision.
1const num = 123.456; 2 3console.log(num.toPrecision(4));
Output
1123.5
parseInt()
Converts a string into an integer.
1console.log(parseInt("100")); 2console.log(parseInt("100px"));
Output
1100 2100
parseFloat()
Converts a string into a floating-point number.
1console.log(parseFloat("99.95"));
Output
199.95
isNaN()
Checks whether a value is Not-a-Number.
1console.log(isNaN("Hello")); 2console.log(isNaN(100));
Output
1true 2false
isFinite()
Checks whether a number is finite.
1console.log(isFinite(100)); 2console.log(isFinite(Infinity));
Output
1true 2false
Mini Project — Random Dice Roller
1const dice = Math.floor(Math.random() * 6) + 1; 2 3console.log(`Dice: ${dice}`);
Possible Output
1Dice: 4
Best Practices
- Use
Math.floor(Math.random() * n)to generate random integers. - Prefer the exponent operator (
**) overMath.pow()in modern JavaScript. - Use
toFixed()for displaying currency values. - Use
parseInt()andparseFloat()to convert user input from forms. - Validate numeric input with
isNaN()orNumber.isNaN()before performing calculations.
Summary
In this tutorial, you learned:
- How to use the
Mathobject. - How to generate random numbers.
- Different rounding methods (
round(),floor(),ceil(),trunc()). - How to find minimum and maximum values.
- How to calculate powers and square roots.
- How to format numbers using
toFixed()andtoPrecision(). - How to convert strings into numbers with
parseInt()andparseFloat(). - How to validate numeric values using
isNaN()andisFinite().
Next Module: Phase 7 — Dates & Time, where you'll learn the JavaScript
Dateobject, timestamps, formatting, time zones, and date manipulation used in real-world applications.