1.Number Types and Conversions:
const score = 300
console.log(score);
const balance = new Number(400)
console.log(balance);
console.log(balance.toString().length);
Here, we're dealing with both primitive numbers and Number objects. The toString()
method helps us convert a number into a string, allowing us to leverage string properties on what was originally a number.
2.Decimal Points and Precision:
console.log(balance.toFixed(2))
const otherNumber = 344.89595;
console.log(otherNumber.toPrecision(3));
The toFixed
method is a handy tool for controlling the number of decimal places, while toPrecision
allows us to specify the total number of digits.
3.Readability with Locale Methods:
const hundred = 1000000
console.log(hundred.toLocaleString()); // Standard comma separation
console.log(hundred.toLocaleString('en-IN')); // Indian numbering system
Enhance the readability of large numbers by using toLocaleString
. You can customize the formatting based on locale, making it more accessible to diverse audiences.
4.Math Functions:
console.log(Math.abs(-4)); // Absolute value
console.log(Math.round(4.6)); // Round to the nearest integer
console.log(Math.ceil(5.2)); // Round up to the nearest integer
console.log(Math.floor(5.9)); // Round down to the nearest integer
console.log(Math.min(3,4,8,1,9)); // Find the minimum value
console.log(Math.max(4,6,1,8,9,10)); // Find the maximum value
console.log(Math.random()); // Generate a random number between 0 and 1
JavaScript's Math
object provides a variety of functions for common mathematical operations. Whether you need absolute values, rounding, or finding the minimum and maximum values in a set, Math
has got you covered.
ย