Have you ever used parseInt to truncate a number in JavaScript? I used to do it too. But today, I want to show you why that habit might be hurting your code – and how to do it better.

parseInt was designed to convert strings to integers, not to truncate numbers. When you write:

parseInt(4.9); // 4 

Does it work? Yes.
Is it clear? Not really.
Is it fast? Definitely not.

⚡️ The Better Way

If you’re working with a number and want to remove the decimal part, use:

Math.trunc(4.9); // 4 

It’s faster, clearer, and semantically correct. It also works with negative numbers:

Math.trunc(-4.9); // -4 

🧪 Performance Matters

In simple benchmarks, Math.trunc can be up to 5x faster than parseInt. That matters – especially in loops, animations, or real-time calculations.

🧨 Other Ways to Truncate Numbers in JavaScript (Use with Caution)

Sometimes you want to truncate a number without using Math.trunc(). Here are some alternatives – each with its own quirks.

🔸 Bitwise OR (| 0)

This is a clever trick to truncate decimals using bitwise operations.

4.9 | 0; // 4 ~~4.9; // 4 

✅ Fast
⚠️ Only works reliably for 32-bit integers. Avoid with large or precise numbers.

🔸 Double Bitwise NOT (~~)

Similar to | 0, but slightly more readable for some developers.

~~4.9; // 4 ~~-4.9; // -4 

✅ Fast
⚠️ Same limitations as | 0.

🔸 Math.floor() and Math.ceil()

These are rounding functions, not true truncation – but they can be useful depending on the sign of the number.

Math.floor(4.9); // 4 Math.floor(-4.9); // -5 Math.ceil(4.9); // 5 Math.ceil(-4.9); // -4 

✅ Clear intent
⚠️ Not truncation – they round up/down.

🔸 Number.toFixed(0)

This method returns a string, not a number, and it rounds the value.

(4.9).toFixed(0); // "5" Number((4.9).toFixed(0)); // 5 

✅ Useful for formatting
⚠️ Not truncation, and returns a string unless converted.

📊 Recommendation

Method Truncates? Returns Performance Notes
Math.trunc() Number 🔼 High Best choice for clarity and safety
parseInt() Number 🔽 Low Avoid for truncating numbers
Math.floor() Number 🔼 High Rounds down, not truncates
Math.ceil() Number 🔼 High Rounds up, not truncates
toFixed(0) String 🔽 Medium Rounds and returns string unless converted

✅ Why Best Practices Matter

As a developer, you’re not just writing for machines – you’re writing for other humans, including your future self. Choosing the right tool makes your code:

  • Easier to read
  • Faster to run
  • More meaningful

💬 Final Thoughts

If you’re truncating numbers with parseInt, stop now. Use Math.trunc and write code that makes sense. Small choices lead to better habits – and better software.

Enjoyed this tip? Share it with a friend who’s still stuck in parseInt land. Let’s do the basics right – then we can do the magic. 😉


Source: DEV Community.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.