parseInt() vs toString() in JavaScript — Explained Like You're 5

pushpesh kumarpushpesh kumar
4 min read

If you’ve ever needed to convert a string to a number, or a number to a string, you’ve likely run into parseInt() and toString().
They look simple, but they come with a secret weapon you need to understand: radix.

Let’s unpack all of it — clearly and practically.


🤔 First of All... What Is Radix?

Think of radix as the base of a number system.

You already use radix 10 every day — that’s the decimal system (0 to 9).
Computers often use other bases too:

Base (Radix)NameDigits UsedExample
2Binary0, 1'1010' (binary) = 10 (decimal)
10Decimal0–9'42' = 42
16Hexadecimal0–9, A–F'ff' = 255

So when you call parseInt('1010', 2), you’re saying:

“Hey JavaScript, this string '1010' is in binary, please convert it to a decimal number.”


🧠 parseInt() — Turning Strings into Integers

🧾 Syntax:

parseInt(string, radix);
  • string → The input you want to convert (like '42', '1010', 'ff')

  • radix → The base of the number system you're using (like 2, 10, 16)

✅ Examples:

parseInt('42', 10);     // 42  ← Simple decimal
parseInt('1010', 2);    // 10  ← Binary to decimal
parseInt('1A', 16);     // 26  ← Hexadecimal to decimal
parseInt('100px', 10);  // 100 ← Stops at first non-digit
parseInt('abc', 10);    // NaN ← Not a number

⚠️ Important: Always include the radix

This is good:

parseInt('08', 10);  // ✅ Output: 8

This is risky:

parseInt('08');      // ❌ Might output 8 or 0 depending on the browser

Older JavaScript engines sometimes treated strings like '08' as octal (base 8), which led to bugs. Be safe — always specify the radix.


🧠 toString() — Turning Numbers into Strings

🧾 Syntax:

number.toString(radix);
  • number → Any number you want to convert

  • radix → (Optional) Which base you want the result in — default is 10.

✅ Examples:

(42).toString();       // "42"
(255).toString(16);    // "ff" ← Decimal to hex
(8).toString(2);       // "1000" ← Decimal to binary

🔍 Why (42).toString() and not 42.toString()?

Because 42.toString() is parsed as a float and gives an error.
Use parentheses or a space like this:

(42).toString(2);  // ✅
42..toString(2);   // ✅ (two dots)
42 .toString(2);   // ✅ (with space)

🧪 Side-by-Side Breakdown

FeatureparseInt()toString()
Converts fromString → NumberNumber → String
Accepts radix?✅ Yes (base 2–36)✅ Yes (base 2–36)
Default radixGuesses (⚠️ risky!)10
Input type'123' (string)123 (number)
Output type123 (number)"123" (string)
ReturnsNumber or NaNString
Stops reading atFirst non-digitN/A

👀 Real-world Use Cases

TaskUse
Get a number from form input like "42"parseInt(inputValue, 10)
Turn a hex color into RGBparseInt('ff', 16)255
Show a binary version of a number(42).toString(2)"101010"
Concatenate with textResult: ${num.toString()} or String(num)

🧠 Gotchas to Watch Out For

MistakeWhy it's badFix
parseInt('3.14')Output: 3 (ignores decimal)Use parseFloat()
parseInt('08')Might return 0 in old enginesAlways write parseInt('08', 10)
parseInt('abc')Output: NaNValidate input first
42.toString()SyntaxErrorUse (42).toString()

🎯 Final Summary

// Convert string to number
parseInt("1010", 2);   // → 10

// Convert number to string
(255).toString(16);    // → "ff"

🧠 Golden Rule:

Use parseInt() when reading a number from a string.
Use toString() when outputting a number as text (especially in binary, hex, etc.).

0
Subscribe to my newsletter

Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pushpesh kumar
pushpesh kumar