← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 5 of 6

JS Numbers

Unlike some languages that keep whole numbers and decimals in separate boxes, JavaScript quietly stores every number, whether it's 5 or 5.5, in the same kind of container: a 64-bit floating-point value. That single unified Number type is convenient most of the time, but it also means precision limits and rounding quirks show up in places a beginner might not expect, which is exactly what this topic walks through.

The Number Type

JavaScript has only one numeric type for both integers and decimals: a 64-bit floating-point format defined by the IEEE 754 standard. There's no separate int or float keyword like in many other languages, so typeof always reports number whether the value is 42 or 3.14159.

Example: The Number Type

javascript
console.log(typeof 42, typeof 3.14159); // both "number", one type for all

Checking Numbers with Number.isInteger

Number.isInteger() checks whether a value is both a number and has no fractional part, returning false for non-numbers instead of coercing them like the older global isFinite() and isNaN() functions do. It's the safer, more predictable choice when you specifically need to know if a value is a whole number.

Example: Checking Numbers with Number.isInteger

javascript
console.log(Number.isInteger(5));    // true
console.log(Number.isInteger(5.5));  // false
console.log(Number.isInteger("5"));  // false, no coercion

Parsing Strings into Numbers

parseInt() and parseFloat() read as many valid numeric characters as they can from the start of a string and ignore the rest, so parseInt(42px) returns 42. Number(), by contrast, requires the entire string to be numeric or it returns NaN, making it stricter but less forgiving of trailing text.

Example: Parsing Strings into Numbers

javascript
console.log(parseInt("42px"));    // 42
console.log(Number("42px"));      // NaN, stricter

Rounding and Formatting with toFixed

toFixed(n) formats a number to n digits after the decimal point and returns the result as a string, not a number, which trips people up if they forget to convert it back. It's commonly used for displaying prices or measurements where a consistent number of decimal places matters more than raw precision.

Example: Rounding and Formatting with toFixed

javascript
const price = 19.5;
console.log(price.toFixed(2)); // "19.50" - a string, not a number

Precision Limits and MAX_SAFE_INTEGER

Number.MAX_SAFE_INTEGER marks the largest whole number JavaScript can represent without losing precision; beyond it, adding 1 may not actually change the value. Because of floating-point rounding, comparing decimal results for exact equality is unreliable, so many programs compare against a tiny tolerance value like Number.EPSILON instead.

Example: Precision Limits and MAX_SAFE_INTEGER

javascript
console.log(Number.MAX_SAFE_INTEGER);
console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2); // true, precision lost
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.