Course contentsShow
← JavaScript
Lesson 1966 of 2,72341. Modern JavaScript Features (ES2020+)Free lesson

Numeric Separator Rules and Restrictions

Learn where underscores can and cannot appear in numeric literals to avoid syntax errors.

Numeric Separator Rules and Restrictions

You'll learn: Where you can and cannot place underscores in numeric literals to keep your code valid and error-free.

The Core Idea

Numeric separators (underscores) exist to improve readability, but JavaScript has strict rules about where they can appear. Understanding these restrictions prevents syntax errors and ensures your code runs smoothly.

Where You CAN Use Underscores

You can place underscores between digits in any numeric literal:

const valid1 = 1_000_000;
const valid2 = 0b1010_1100;
const valid3 = 0x1A_2B_3C;
const valid4 = 3.14_15_92;

You can use multiple underscores or place them anywhere between digits—whatever makes the number clearest.

Where You CANNOT Use Underscores

At the start or end:

const bad1 = _1000;      // ❌ SyntaxError
const bad2 = 1000_;      // ❌ SyntaxError

Before or after the decimal point:

const bad3 = 3._14;      // ❌ SyntaxError
const bad4 = 3_.14;      // ❌ SyntaxError

After base prefixes (0x, 0b, 0o):

const bad5 = 0x_1A2B;    // ❌ SyntaxError
const bad6 = 0b_1010;    // ❌ SyntaxError

Consecutive underscores are actually allowed (though not necessarily recommended):

const allowed = 1__000__000;  // ✅ Valid but odd

Think of It Like Commas in Writing

Just as you wouldn't write ",1000" or "1,000," or "3,.14" in English, underscores must sit comfortably between the meaningful parts of your number.

Key Takeaway: Numeric separators must appear only between digits—never at boundaries like the start, end, decimal point, or base prefix.