What is JavaScript Numbers ?
JavaScript numbers are a fundamental data type used for mathematical operations and handling numerical data. JavaScript treats all numbers as floating-point (decimal) numbers, making it versatile for different calculations. This guide covers JavaScript numbers, their properties, methods and common use cases with coding examples.
Number Types in JavaScript
JavaScript does not differentiate between integers and floating-point numbers. All numbers, regardless of their type, are stored in a 64-bit IEEE 754 floating-point format.
- Integer: Whole numbers without a decimal point (e.g., 10, -5).
- Floating-Point Number: Numbers with a decimal point (e.g., 10.5, -3.14).
- Exponential Notation: Large or small numbers represented using e (e.g., 1.5e3 for 1500).
Examples of JavaScript Numbers
let integer = 25; // Integer
let floating = 3.14; // Floating-point number
let exponential = 2.5e6; // 2.5 × 10^6 = 2500000
let negative = -42; // Negative number
Number Properties in JavaScript
JavaScript provides built-in properties for numbers:
Property | Description | Example |
---|---|---|
Number.MAX_VALUE | Largest positive number in JavaScript. | 1.7976931348623157e+308 |
Number.MIN_VALUE | Smallest positive number close to zero. | 5e-324 |
Number.POSITIVE_INFINITY | Represents infinity. | Infinity |
Number.NEGATIVE_INFINITY | Represents negative infinity. | -Infinity |
Number.NaN | Represents “Not-a-Number”. | NaN |
Number Methods
JavaScript provides several methods to work with numbers effectively.
1. toString()
Converts a number to a string.
let num = 42;
console.log(num.toString()); // Output: "42"
2. toFixed()
Formats a number with a specific number of decimals.
let price = 25.6789;
console.log(price.toFixed(2)); // Output: "25.68"
3. toExponential()
Converts a number to exponential notation.
let num = 123456;
console.log(num.toExponential(2)); // Output: "1.23e+5"
4. toPrecision()
Formats a number to a specified length.
let num = 5.6789;
console.log(num.toPrecision(3)); // Output: "5.68"
5. Number.isNaN()
Checks if a value is NaN.
console.log(Number.isNaN(42)); // Output: false
console.log(Number.isNaN("hello" / 2)); // Output: true
6. Number.isFinite()
Checks if a value is finite.
console.log(Number.isFinite(25)); // Output: true
console.log(Number.isFinite(Infinity)); // Output: false
Number Conversions
Use these methods to convert values to numbers.
1. Number()
Converts a value to a number.
let str = "123";
console.log(Number(str)); // Output: 123
2. parseInt()
Parses a string and returns an integer.
let str = "42px";
console.log(parseInt(str)); // Output: 42
3. parseFloat()
Parses a string and returns a floating-point number.
let str = "42.5px";
console.log(parseFloat(str)); // Output: 42.5
Special Values
1. Infinity
Represents a value larger than Number.MAX_VALUE.
console.log(1 / 0); // Output: Infinity
2. NaN (Not-a-Number)
Indicates an invalid mathematical operation.
console.log("hello" / 2); // Output: NaN
Example: Practical Usage of Numbers
Calculating Discounts
let price = 100;
let discount = 20; // Percentage
let finalPrice = price - (price * (discount / 100));
console.log(`The final price after discount is ${finalPrice}.`);
// Output: The final price after discount is 80.
Converting Strings to Numbers
let str = "50";
let num = Number(str);
console.log(num + 10); // Output: 60
Validating User Input
function isValidNumber(value) {
return !Number.isNaN(Number(value));
}
console.log(isValidNumber("42")); // Output: true
console.log(isValidNumber("hello")); // Output: false
Common Mistakes
- Mixing Strings and Numbers: JavaScript may perform implicit type conversion, leading to unexpected results.
console.log("10" + 20); // Output: "1020" (string concatenation)
console.log("10" - 5); // Output: 5 (type conversion)
- Comparing with NaN: Always use Number.isNaN() instead of equality checks.
console.log(NaN === NaN); // Output: false
console.log(Number.isNaN(NaN)); // Output: true