Course contentsShow
← JavaScript
Lesson 1595 of 2,72335. Standard Library and Built-insFree lesson

Math.trunc for Integer Extraction

Understand how Math.trunc removes the decimal portion without rounding, useful for integer conversions.

Math.trunc for Integer Extraction

You'll learn how Math.trunc() removes the decimal portion of a number without rounding, giving you a clean integer.

What Math.trunc Does

Unlike Math.round(), Math.floor(), and Math.ceil() which involve rounding logic, Math.trunc() simply chops off the decimal part of a number. Think of it like cutting a piece of paper with scissors—you're not measuring or deciding which way to round, you're just making a straight cut at the decimal point.

For positive numbers, it acts like Math.floor(). For negative numbers, it acts like Math.ceil(). But the key difference is the intent: you're extracting the integer portion, not rounding toward any direction.

How It Works

Math.trunc(4.9);    // 4 (decimal removed, not rounded)
Math.trunc(4.1);    // 4
Math.trunc(-4.9);   // -4 (not -5!)
Math.trunc(-4.1);   // -4
Math.trunc(0.123);  // 0

Notice that Math.trunc(-4.9) returns -4, not -5. It's removing .9, not rounding down.

When to Use Math.trunc

Use Math.trunc() when you need the integer part of a number and don't care about rounding logic:

const bytes = 1234.56;
const kilobytes = Math.trunc(bytes / 1024); // Just want the whole KB

const price = 19.99;
const dollars = Math.trunc(price); // Extract dollar amount: 19

Key Takeaway: Math.trunc() removes the decimal portion without any rounding, making it the clearest choice when you simply need to extract the integer part of a number—it always moves toward zero.