JavaScript Number

Beginner
8 min read
Updated: Nov 2026
5 examples
IEEE-754

Definition and Usage

In JavaScript, a Number is a numeric value: integers and floating-point numbers such as 42, 3.14, and -7. There is one built-in numeric type for everyday math (IEEE-754 double precision)—not separate int and float types like some other languages.

You write numbers as literals, convert strings with Number() or parseInt(), and use operators like +, -, *, and /. Special values include NaN (invalid math) and Infinity (overflow).

This page teaches what a Number is. The Number Methods index and Number Properties index list every method and constant guide (toFixed(), isInteger(), MAX_SAFE_INTEGER, and more).

Prefer primitives.

Use Number(value) or a literal—not new Number(value). Object wrappers make typeof return "object".

Safe integers.

Exact integer math is reliable only up to Number.MAX_SAFE_INTEGER (2**53 - 1). Beyond that, use BigInt.

JavaScript Syntax

Common ways to create and inspect numbers:

JavaScript
const n = 42;                 // integer literal
const x = 3.14;               // floating-point literal
const fromStr = Number("42"); // convert string → number
const typed = typeof n;       // "number"
const bad = Number("hi");     // NaN
const huge = 1 / 0;           // Infinity

Core Ideas

Ideas every JavaScript Number tutorial builds on:

IdeaExampleRemember
Literal42 / 3.14Most common form
ConversionNumber("10")Returns a primitive
typeoftypeof 5Always "number" (incl. NaN)
NaNNumber("x")Use Number.isNaN
Infinity1 / 0Overflow past MAX_VALUE
Formatting(3.14159).toFixed(2)Returns a string

JavaScript Examples

Five short programs that define how Number works in a JS tutorial.

Example 1 — Create and print

Assign number literals and log them.

JavaScript
const age = 25;
const price = 19.99;
const negative = -7;

console.log("Age:", age);
console.log("Price:", price);
console.log("Negative:", negative);
console.log("typeof age:", typeof age);

How It Works

  1. Literals create number primitives directly.
  2. typeof returns "number" for integers and floats.
  3. Negative values use a leading minus sign.

Example 2 — Convert strings with Number()

Turn text into numbers for calculations.

JavaScript
const a = Number("42");
const b = Number("3.5");
const c = Number("");
const d = Number("hello");

console.log(a, typeof a);
console.log(b);
console.log(c);  // empty string → 0
console.log(d);  // invalid → NaN
Number() Tutorial

How It Works

  1. Number(string) parses the whole string as a number.
  2. Empty string becomes 0.
  3. Invalid text becomes NaN.

Example 3 — Arithmetic operators

Everyday math with +, -, *, and /.

JavaScript
const x = 10;
const y = 3;

console.log("Sum:", x + y);
console.log("Diff:", x - y);
console.log("Product:", x * y);
console.log("Quotient:", x / y);
console.log("Remainder:", x % y);

How It Works

  1. Division can return a float even when both sides are integers.
  2. % is the remainder operator.
  3. Floating-point quirks (like 0.1 + 0.2) are normal in JS.

Example 4 — NaN and Infinity

Special Number values every beginner should recognize.

JavaScript
const bad = 0 / 0;
const huge = 1 / 0;
const tiny = -1 / 0;

console.log(bad);                      // NaN
console.log(Number.isNaN(bad));        // true
console.log(bad === bad);              // false!
console.log(huge);                     // Infinity
console.log(Number.isFinite(huge));    // false
console.log(tiny);                     // -Infinity
isNaN() Tutorial

How It Works

  1. NaN never equals itself—use Number.isNaN.
  2. Infinity appears after overflow or dividing by zero.
  3. isFinite rejects both Infinity and NaN.

Example 5 — Format with toFixed() and toString()

Turn numbers into display strings.

JavaScript
const price = 19.999;
const rgb = 255;

console.log(price.toFixed(2));   // "20.00"
console.log(rgb.toString(16));   // "ff"
console.log(rgb.toString(2));    // "11111111"

How It Works

  1. toFixed rounds to a fixed count of decimals (returns a string).
  2. toString(16) converts to hexadecimal.
  3. More methods are listed on the methods index.

Next: Methods & Properties Indexes

After you know what a Number is, open the indexes for full tutorials on every essential Number API in this JavaScript series.

Start withWhy
toFixed()Display money and decimals
toString()Base conversion (binary, hex)
isInteger()Validate whole numbers
MAX_SAFE_INTEGERExact integer limit

Full lists: Number Methods index and Number Properties index.

Frequently Asked Questions

Number is the built-in type for numeric values—integers and floating-point numbers such as 42, 3.14, and -7. Under the hood it uses IEEE-754 double precision (one Number type for both).
Use a literal: const n = 42; or const x = 3.14;. You can also convert with Number("42"), parseInt("42", 10), or unary plus: +"42".
NaN means Not-A-Number—the result of invalid math like 0 / 0 or Number("hello"). NaN !== NaN. Detect it with Number.isNaN(value).
Call Number(value) to get a number primitive. Avoid new Number(value)—it creates a Number object and typeof becomes "object", which confuses === checks.
Practice the examples here, then open the Number Methods index for toFixed(), toString(), isInteger(), and the Number Properties index for MAX_SAFE_INTEGER, EPSILON, and Infinity.

Browse every Number method

Open the methods index for categorized guides with examples and FAQs.

Number Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful