JS Built-ins
← Math212 / 215
Mathtruncintermediate

Math.trunc() β€” trunc(-4.9) = -4

πŸ“– Usage Example
// Remove fractional part (truncates towards zero)
Math.trunc(4.9)    // β†’ 4
Math.trunc(-4.9)   // β†’ -4

Use Math.trunc() to drop the decimal part of a number, always truncating towards zero unlike Math.floor().

Math.trunc()

Returns the integer part of a number by removing any fractional digits.

Challenge: Verify that Math.trunc(-4.9) returns -4, not -5 (truncates toward zero, unlike Math.floor).

πŸ’‘ Hints (1)
  • β€’ Math.trunc truncates toward zero: -4.9 β†’ -4, not -5. Compare with Math.floor(-4.9) = -5