H.dev

The Number Object

Working with Numbers

In JavaScript, all numbers are stored as double-precision 64-bit floating-point numbers. This means there is no separate data type for integers and decimals (floats)—everything is just a "Number".

The Number object provides several useful properties and methods to help you format and inspect numeric data.

Essential Number Methods

1. toFixed() This is arguably the most useful number method. It formats a number with a specific number of decimals and returns it as a String. This is perfect for formatting currency!

let price = 9.5678;
console.log(price.toFixed(2)); // "9.57"

2. Number.isInteger() Checks whether a value is a whole number (an integer) or a decimal.

console.log(Number.isInteger(10));   // true
console.log(Number.isInteger(10.5)); // false

3. Number.parseFloat() and Number.parseInt() These methods are used to extract numbers from strings.

  • parseInt() extracts a whole number.
  • parseFloat() extracts a decimal number.
let cssWidth = "150.5px";
console.log(Number.parseInt(cssWidth));   // 150
console.log(Number.parseFloat(cssWidth)); // 150.5

NaN (Not a Number)

NaN is a special property of the Number object that indicates a value is not a legal number. This usually happens if you try to perform math on a string that doesn't contain a number.

let result = 100 / "Apple";
console.log(result); // NaN
console.log(Number.isNaN(result)); // true

💡 Himanshu's Tip:

Never trust decimal math blindly in JavaScript! Because of how floating-point numbers work in memory, 0.1 + 0.2 actually equals 0.30000000000000004. If you are building an e-commerce app, always store money in "cents" (whole numbers) and divide by 100 when displaying it to the user!

Interview Questions

  1. What does NaN stand for, and what data type is it technically classified as?
  2. Why does 0.1 + 0.2 === 0.3 evaluate to false in JavaScript?
  3. What is the difference between Math.round() and Number.toFixed()?

Design & Developed by Himanshu Dubey © 2026