This chapter delves into the treasure trove of built-in methods available in JavaScript to manipulate and interact with numbers. We'll explore methods for basic operations, advanced conversions, and even number-related checks.
toString(radix)
: Converts a number to a string representation in the specified radix (base). The radix can be any integer between 2 and 36.
const num = 255;
const binaryString = num.toString(2); // binaryString will be "11111111"
const octalString = num.toString(8); // octalString will be "377"
const hexadecimalString = num.toString(16); // hexadecimalString will be "ff"
console.log(binaryString);
console.log(octalString);
console.log(hexadecimalString);
toFixed(decimals)
: Formats a number with a specified number of decimal places.
const pi = 3.14159;
const formattedPi = pi.toFixed(2); // formattedPi will be "3.14"
const moneyValue = 12345.6789;
const formattedMoney = moneyValue.toFixed(2); // formattedMoney will be "12345.68"
console.log(formattedPi);
console.log(formattedMoney);
toExponential(fractionDigits)
: Converts a number to exponential notation with a specified number of fractional digits after the decimal point.
const largeNum = 9876543210;
const expNotation = largeNum.toExponential(3); // expNotation will be "9.876e+9"
console.log(expNotation);
valueOf()
: Returns the primitive value of the number (useful for interoperability with other data types).
const numObj = new Number(123.45);
const stringValue = numObj.toString(); // toString() uses valueOf() internally
const floatValue = parseFloat(numObj.valueOf()); // Explicit use of valueOf()
console.log(stringValue); // Output: "123.45" (string representation)
console.log(floatValue); // Output: 123.45 (floating-point number)
parseInt(string, radix)
: Parses a string argument and returns an integer based on the specified radix (base).
const binaryString = "1100";
const decimalNum = parseInt(binaryString, 2); // decimalNum will be 12
const hexString = "FF";
const colorValue = parseInt(hexString, 16); // colorValue will be 255 (assuming used for color representation)
console.log(decimalNum);
console.log(colorValue);
parseFloat(string)
: Parses a string argument and returns a floating-point number.
const strNum = "3.14abc";
const parsedFloat = parseFloat(strNum); // parsedFloat will be 3.14 (ignores "abc")
console.log(parseFloat);
isNaN(number)
: Determines whether a value is Not a Number (NaN).
const result1 = isNaN(10); // result1 will be false
const result2 = isNaN(Number("hello")); // result2 will be true
const result3 = isNaN(Math.sqrt(-1)); // result3 will be true (square root of a negative number)
console.log(result1);
console.log(result2);
console.log(result3);
isFinite(number)
: Determines whether a number is a finite number (not NaN, positive or negative infinity).
const result1 = isFinite(10); // result1 will be true
const result2 = isFinite(Infinity); // result2 will be false
const result3 = isFinite(NaN); // result3 will be false
console.log(result1);
console.log(result2);
console.log(result3);
Math.pow(x, y)
: Raises x
to the power of y
.Math.sqrt(x)
: Square root of x
.Math.min(x1, x2, ...)
: Returns the smallest of the provided numbers.Math.max(x1, x2, ...)
: Returns the largest of the provided numbers.Math.random()
: Generates a pseudo-random number between 0 (inclusive) and 1 (exclusive).Number.isInteger(number)
(ES6+): Determines whether a number is an integer (no decimal part).
const num1 = 25;
const num2 = 3.14;
const isInt1 = Number.isInteger(num1); // isInt1 will be true
const isInt2 = Number.isInteger(num2); // isInt2 will be false
console.log(isInt1);
console.log(isInt2);
Number.EPSILON
(ES6+): Represents the smallest representable difference between two distinct positive numbers. Useful for floating-point precision comparisons.Math
object for advanced mathematical calculations.By effectively utilizing these methods, you can master the art of working with numbers in JavaScript, ensuring your code is not only functional but also performs as expected.
BigInt
data type introduced in ES2020 for precise calculations beyond the limitations of standard numbers.I hope this comprehensive explanation empowers you to confidently use number methods in your JavaScript projects!
JavaScript's built-in number methods offer a versatile toolkit for manipulating, converting, and analyzing numbers. By understanding these methods, you can enhance the precision, readability, and efficiency of your code. Happy coding !❤️