JavaScript Numbers — Interview Questions & Answers
Ques: What are Numbers in JavaScript?
Ans: In JavaScript, numbers represent both integers and floating-point values. Unlike many languages, JavaScript has only one number type:
64-bit IEEE 754 floating-point.
Example:
let x = 10; // integer
let y = 10.5; // floating-point
let z = 1.2e6; // exponential (1.2 × 10⁶)
Ques: What are the different types of numeric values in JavaScript?
| Type | Example | Description |
|---|---|---|
| Integer | 42 | Whole numbers |
| Floating-point | 3.14 | Decimal values |
| Exponential | 1.2e5 | Scientific notation |
| Hexadecimal | 0xFF | Base 16 |
| Binary | 0b1010 | Base 2 |
| Octal | 0o17 | Base 8 |
Ques: What is the type of a number in JavaScript?
typeof 42; // "number"
typeof 3.14; // "number"
Ques: What is NaN?
Ans: NaN stands for Not-a-Number. It indicates that a value is not a valid number.
Example:
console.log(100 / "Apple"); // NaN
console.log(typeof NaN); // "number"
Ques: How to check if a value is NaN?
isNaN("Hello"); // true
Number.isNaN(100); // false
Number.isNaN(NaN); // true
Ques: What is Infinity and -Infinity?
- Infinity represents a value larger than any number.
- Infinity represents a value smaller than any number.
Example:
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(typeof Infinity); // "number"
Ques: What is Number.MAX_VALUE and Number.MIN_VALUE?
| Property | Description |
|---|---|
Number.MAX_VALUE | Largest representable number |
Number.MIN_VALUE | Smallest positive representable number |
Example:
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324
Ques: What is the difference between == and === for numbers?
10 == "10"; // true (type coercion)
10 === "10"; // false (strict check)
Ques: How to convert strings to numbers?
| Method | Example | Output |
|---|---|---|
Number() | Number("10") | 10 |
parseInt() | parseInt("10.5") | 10 |
parseFloat() | parseFloat("10.5") | 10.5 |
Unary + | +"20" | 20 |
Ques: How to check if a value is finite or infinite?
isFinite(100); // true
isFinite(Infinity); // false
Ques: What are Number methods in JavaScript?
| Method | Description | Example | Output |
|---|---|---|---|
toString() | Converts to string | (123).toString() | "123" |
toExponential() | Converts to exponential form | (12345).toExponential(2) | "1.23e+4" |
toFixed() | Formats to fixed decimals | (3.14159).toFixed(2) | "3.14" |
toPrecision() | Formats to total digits | (3.14159).toPrecision(3) | "3.14" |
valueOf() | Returns primitive value | (123).valueOf() | 123 |
Ques: What is the difference between parseInt() and Number()?
| Function | Behavior | Example | Output |
|---|---|---|---|
parseInt() | Parses until non-digit | parseInt("10px") | 10 |
Number() | Converts entire string | Number("10px") | NaN |
Ques: What are Number constants in JavaScript?
| Constant | Description |
|---|---|
Number.EPSILON | Smallest difference between two representable numbers |
Number.MAX_SAFE_INTEGER | 9007199254740991 |
Number.MIN_SAFE_INTEGER | -9007199254740991 |
Number.POSITIVE_INFINITY | Infinity |
Number.NEGATIVE_INFINITY | -Infinity |
Number.NaN | NaN |
Ques: What is EPSILON in JavaScript?
Ans: Used to compare floating-point precision safely.
let a = 0.1 + 0.2;
console.log(Math.abs(a - 0.3) < Number.EPSILON); // true
Ques: What is BigInt in JavaScript?
Ans: BigInt allows representation of numbers larger than 2⁵³ - 1 (the max safe integer).
Example:
let big = 123456789012345678901234567890n;
console.log(typeof big); // "bigint"
BigInts can’t be mixed with regular numbers directly:
10n + 5; // TypeError
10n + 5n; // 15n
Ques: How can you generate random numbers in JavaScript?
Ans: Using Math.random() (returns a number between 0 and 1).
Math.random(); // e.g., 0.7391
Math.floor(Math.random() * 10); // 0–9
Math.floor(Math.random() * 100) + 1; // 1–100
Ques: How can you round numbers in JavaScript?
| Function | Description | Example | Output |
|---|---|---|---|
Math.round() | Rounds to nearest | Math.round(4.6) | 5 |
Math.floor() | Rounds down | Math.floor(4.9) | 4 |
Math.ceil() | Rounds up | Math.ceil(4.1) | 5 |
Math.trunc() | Removes decimals | Math.trunc(4.9) | 4 |
Ques: What is the difference between isNaN() and Number.isNaN()?
| Function | Behavior | Example | Output |
|---|---|---|---|
isNaN("Hello") | Converts value before check | true | |
Number.isNaN("Hello") | Strict check, no conversion | false |
Ques: How to check if a number is an integer?
Number.isInteger(10); // true
Number.isInteger(10.5); // false
Ques: What happens when you divide by zero in JavaScript?
Ans: You get Infinity (or -Infinity) instead of an error.
10 / 0; // Infinity
-10 / 0; // -Infinity
Ques: What is Number.parseInt() vs parseInt()?
Both are same; Number.parseInt() is the ES6 modular version of global parseInt().
Ques: How to check if a number is safe?
Number.isSafeInteger(10); // true
Number.isSafeInteger(2 ** 60); // false
Ques: Can you perform arithmetic with BigInt?
Ans: Yes, but only between BigInts — not mixed with numbers.
let a = 100n, b = 20n;
console.log(a + b); // 120n
Ques: How to fix floating-point precision errors?
let result = (0.1 + 0.2).toFixed(2);
console.log(result); // "0.30"
or
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON; // true
