JavaScript Numbers
What are Numbers in JavaScript?
In JavaScript, numbers are a data type used to represent both:
- Integers (like 10, -5, 1000)
- Floating-point numbers (like 3.14, -0.01, 2.718)
In JavaScript, there is only one type for all numbers: number. (Unlike some languages that have different types for integers and floats.)
Creating Numbers
1. Using Numeric Literals
let x = 10; // Integer
let y = 3.14; // Floating-point number
2. Using the Number Object
let num = new Number(100);
console.log(num); // Output: [Number: 100]
Note: Using new Number() creates a Number object, not a primitive number. Mostly, primitive numbers are preferred.
Examples and Outputs
Example 1: Basic Numbers
let a = 5;
let b = 2.5;
console.log(a); // Output: 5
console.log(b); // Output: 2.5
Example 2: Arithmetic Operations
let x = 10;
let y = 4;
console.log(x + y); // Output: 14
console.log(x - y); // Output: 6
console.log(x * y); // Output: 40
console.log(x / y); // Output: 2.5
console.log(x % y); // Output: 2
Special Number Values
Value | Description | Example | Output |
---|---|---|---|
Infinity | Result of division by 0 | 5 / 0 | Infinity |
-Infinit Y | Negative division by 0 | -5 / 0 | -Infinit Y |
NaN | "Not a Number" (Invalid calculation) | "abc" / 2 | NaN |
Example:
console.log(5 / 0); // Output: Infinity
console.log(-5 / 0); // Output: -Infinity
console.log("abc" / 2); // Output: NaN
Checking Numbers
1. isNaN()
Checks if a value is NaN (Not a Number).
console.log(isNaN(100)); // Output: false
console.log(isNaN("hello")); // Output: true
2. isFinite()
Checks if a number is finite.
console.log(isFinite(10)); // Output: true
console.log(isFinite(Infinity)); // Output: false
Number Methods
Method | Description | Example | Output |
---|---|---|---|
toString() | Converts number to string | (123).toString() | "123" |
toFixed(n) | Rounds number with n decimals | (3.14159).toFixed(2) | "3.14" |
toExponential() | Converts to exponential form | (123456).toExponential 5 | "1.23456e+"1.23456e+5" |
toPrecision(n) | Formats to specified length | (3.14159).toPrecision(4) | "3.142" |
Example:
let num = 5.6789;
console.log(num.toFixed(2)); // Output: "5.68"
console.log(num.toString()); // Output: "5.6789"
console.log(num.toExponential(2)); // Output: "5.68e+0"
console.log(num.toPrecision(3)); // Output: "5.68"
Number Constants
JavaScript provides built-in constants related to numbers:
Constant | Value | Description |
---|---|---|
Number.MAX_VALUE | Largest possible number | |
Number.MIN_VALUE | Smallest positive number | |
Number.POSITIVE_INFINITY | Infinity | |
Number.NEGATIVE_INFINITY | -Infinity | |
Number.NaN | NaN |
Example:
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // Output: 5e-324
Type Conversion to Numbers
You can convert other types into numbers using:
Function | Example | Output |
---|---|---|
Number() | Number("123") | 123 |
parseInt() | parseInt("123px") | 123 |
parseFloat() | parseFloat("3.14meters") | 3.14 |
Example:
console.log(Number("123")); // Output: 123
console.log(parseInt("456abc")); // Output: 456
console.log(parseFloat("3.14xyz")); // Output: 3.14