Exercise
toFixed() formats a number using fixed-point notation, returning a string with a specified number of decimal places. It rounds the result if needed.
let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
console.log(num.toFixed(0)); // "3"
console.log(num.toFixed(4)); // "3.1416"
let price = 5.1;
console.log(price.toFixed(2)); // "5.10"
toPrecision() returns a string representing the number to a specified number of significant digits. Unlike toFixed(), it counts digits on both sides of the decimal point.
let num = 123.456;
console.log(num.toPrecision(5)); // "123.46"
console.log(num.toPrecision(4)); // "123.5"
console.log(num.toPrecision(2)); // "1.2e+2"
let small = 0.000123;
console.log(small.toPrecision(2)); // "0.00012"
Number.isFinite() checks if the value is a finite number without any type coercion - it returns false for non-number types. The global isFinite() coerces the value to a number first.
console.log(Number.isFinite(42)); // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite("42")); // false (no coercion)
console.log(Number.isFinite(NaN)); // false
console.log(isFinite("42")); // true (coerces "42" to 42)
console.log(isFinite("hello")); // false (coerces to NaN)
Number.isNaN() returns true only when the value is exactly NaN - it does not coerce the argument. The global isNaN() coerces its argument to a number first, which can cause unexpected results.
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN("NaN")); // false (string, not NaN)
console.log(Number.isNaN(undefined)); // false (no coercion)
console.log(isNaN("NaN")); // true (coerces to NaN)
console.log(isNaN(undefined)); // true (coerces to NaN)
Number.MAX_SAFE_INTEGER is the largest integer that can be represented exactly in JavaScript (2^53 - 1 = 9007199254740991). Beyond this value, integer arithmetic may lose precision.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 (same!)
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER)); // true
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1)); // false
Number.MIN_SAFE_INTEGER is the most negative safe integer in JavaScript (-(2^53 - 1) = -9007199254740991). It is the negative counterpart of Number.MAX_SAFE_INTEGER.
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
console.log(Number.MIN_SAFE_INTEGER - 1); // -9007199254740992
console.log(Number.MIN_SAFE_INTEGER - 2); // -9007199254740992 (same!)
The toString() method on a number converts it to a string in a given base (radix). Common bases are 2 (binary), 8 (octal), 10 (decimal, default), and 16 (hexadecimal).
let num = 255;
console.log(num.toString()); // "255" (base 10, default)
console.log(num.toString(2)); // "11111111" (binary)
console.log(num.toString(8)); // "377" (octal)
console.log(num.toString(16)); // "ff" (hexadecimal)
JavaScript uses IEEE 754 double-precision floating point, which cannot precisely represent all decimal fractions in binary. This causes small rounding errors in arithmetic operations.
console.log(0.1 + 0.2); // 0.30000000000000004 (not 0.3!)
console.log(0.1 + 0.2 === 0.3); // false
// Workarounds:
console.log((0.1 + 0.2).toFixed(1)); // "0.3"
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true
These three methods round a number differently: Math.round() rounds to the nearest integer (0.5 goes up), Math.floor() always rounds down, and Math.ceil() always rounds up.
console.log(Math.round(4.5)); // 5
console.log(Math.round(4.4)); // 4
console.log(Math.floor(4.9)); // 4
console.log(Math.floor(-4.1)); // -5
console.log(Math.ceil(4.1)); // 5
console.log(Math.ceil(-4.9)); // -4
Math.abs() returns the absolute (non-negative) value of a number. It is useful when you need the magnitude of a difference, regardless of direction.
console.log(Math.abs(-5)); // 5
console.log(Math.abs(5)); // 5
console.log(Math.abs(0)); // 0
// Check if two numbers are close
function almostEqual(a, b, tolerance = 0.001) {
return Math.abs(a - b) <= tolerance;
}
console.log(almostEqual(0.1 + 0.2, 0.3)); // true