Day 6: Numbers & Math Made Easy

๐ฃ Working with Numbers
const score = 400;
console.log(score); // 400
const balance = new Number(100);
console.log(balance); // [Number: 100]
console.log(balance.toString()); // "100"
console.log(balance.toString().length); // 3
console.log(balance.toFixed(1)); // "100.0"
Method | Description | Example | Output |
.toString() | Converts number to string | 100.toString() | "100" |
.toFixed(n) | Formats to n decimals | 100.456.toFixed(2) | "100.46" |
.toPrecision(n) | Total digits including before/after decimal | 123.8966.toPrecision(4) | "123.9" |
.toLocaleString() | Adds comma separators | 1000000.toLocaleString('en-IN') | "10,00,000" |
๐งฎ Example:
const otherNumber = 123.8966;
console.log(otherNumber.toPrecision(4)); // "123.9"
const hundreds = 1000000;
console.log(hundreds.toLocaleString('en-IN')); // "10,00,000"
๐ข JavaScript Math Object
console.log(Math); // Full Math object
๐ง Common Math Methods
Method | Description | Example | Output |
Math.abs() | Absolute value | Math.abs(-4) | 4 |
Math.round() | Rounds to nearest | Math.round(4.6) | 5 |
Math.ceil() | Rounds up | Math.ceil(4.2) | 5 |
Math.floor() | Rounds down | Math.floor(4.9) | 4 |
Math.min() | Smallest value | Math.min(4, 3, 6, 8) | 3 |
Math.max() | Largest value | Math.max(4, 3, 6, 8) | 8 |
Math.random() | Random float (0 to <1) | Math.random() | 0.321... |
Math.pow(a, b) | Power a^b | Math.pow(2, 3) | 8 |
Math.sqrt() | Square root | Math.sqrt(16) | 4 |
Math.trunc() | Removes decimal | Math.trunc(4.9) | 4 |
Math.sign() | Returns sign (1/-1/0) | Math.sign(-10) | -1 |
๐ฒ Generating Random Numbers
console.log(Math.random()); // 0 to 0.999...
console.log((Math.random() * 10) + 1); // 1 to 10.999...
console.log(Math.floor(Math.random() * 10) + 1); // 1 to 10
๐ง Random Number Between a Range (min to max)
const min = 10;
const max = 20;
console.log(Math.floor(Math.random() * (max - min + 1)) + min);
โ This gives you a number between 10 and 20 (inclusive).
โ Interview Q&A
Whatโs the difference between
toFixed()
andtoPrecision()
?
โtoFixed()
fixes decimal places,toPrecision()
controls total significant digits.How to round a number to the nearest integer in JavaScript?
โMath.round(number)
How to generate a number between 1 and 100 randomly?
โMath.floor(Math.random() * 100) + 1
Whatโs the output of
Math.floor(Math.random() * 10) + 1
?
โ A random number between 1 and 10.Whatโs the difference between
Math.floor()
andMath.trunc()
?
โfloor()
always rounds down,trunc()
removes the decimal part (likeparseInt()
).
๐ง Homework Practice
Try these and predict results:
console.log((12345.6789).toLocaleString()); console.log((12345.6789).toFixed(2)); console.log((12345.6789).toPrecision(6));
Generate:
Random number between 50 and 100
Round
3.14159
to 2 decimal placesFind max and min of
[13, 7, 21, 4, 9]
Bonus Challenge:
- Build a function
rollDice()
that returns a number from 1 to 6.
- Build a function
๐ GitHub Repo
๐ Day 6 Code on GitHub
Subscribe to my newsletter
Read articles from Souvik Sural directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Souvik Sural
Souvik Sural
Campus Maven at RiseIn Postman Student Expert Web Developer and Blockchain enthusiast