J

JavaScript Handbook

Clean • Professional

JavaScript Numbers — Top Interview Questions & Answers

4 minute

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?

TypeExampleDescription
Integer42Whole numbers
Floating-point3.14Decimal values
Exponential1.2e5Scientific notation
Hexadecimal0xFFBase 16
Binary0b1010Base 2
Octal0o17Base 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?

PropertyDescription
Number.MAX_VALUELargest representable number
Number.MIN_VALUESmallest 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?

MethodExampleOutput
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?

MethodDescriptionExampleOutput
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()?

FunctionBehaviorExampleOutput
parseInt()Parses until non-digitparseInt("10px")10
Number()Converts entire stringNumber("10px")NaN

Ques: What are Number constants in JavaScript?

ConstantDescription
Number.EPSILONSmallest difference between two representable numbers
Number.MAX_SAFE_INTEGER9007199254740991
Number.MIN_SAFE_INTEGER-9007199254740991
Number.POSITIVE_INFINITYInfinity
Number.NEGATIVE_INFINITY-Infinity
Number.NaNNaN

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?

FunctionDescriptionExampleOutput
Math.round()Rounds to nearestMath.round(4.6)5
Math.floor()Rounds downMath.floor(4.9)4
Math.ceil()Rounds upMath.ceil(4.1)5
Math.trunc()Removes decimalsMath.trunc(4.9)4

Ques: What is the difference between isNaN() and Number.isNaN()?

FunctionBehaviorExampleOutput
isNaN("Hello")Converts value before checktrue 
Number.isNaN("Hello")Strict check, no conversionfalse 

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

 

Article 0 of 0