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

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) | Name | Digits Used | Example |
2 | Binary | 0, 1 | '1010' (binary) = 10 (decimal) |
10 | Decimal | 0–9 | '42' = 42 |
16 | Hexadecimal | 0–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
Feature | parseInt() | toString() |
Converts from | String → Number | Number → String |
Accepts radix? | ✅ Yes (base 2–36) | ✅ Yes (base 2–36) |
Default radix | Guesses (⚠️ risky!) | 10 |
Input type | '123' (string) | 123 (number) |
Output type | 123 (number) | "123" (string) |
Returns | Number or NaN | String |
Stops reading at | First non-digit | N/A |
👀 Real-world Use Cases
Task | Use |
Get a number from form input like "42" | parseInt(inputValue, 10) |
Turn a hex color into RGB | parseInt('ff', 16) → 255 |
Show a binary version of a number | (42).toString(2) → "101010" |
Concatenate with text | Result: ${num.toString()} or String(num) |
🧠 Gotchas to Watch Out For
Mistake | Why it's bad | Fix |
parseInt('3.14') | Output: 3 (ignores decimal) | Use parseFloat() |
parseInt('08') | Might return 0 in old engines | Always write parseInt('08', 10) |
parseInt('abc') | Output: NaN | Validate input first |
42.toString() | SyntaxError | Use (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.
UsetoString()
when outputting a number as text (especially in binary, hex, etc.).
Subscribe to my newsletter
Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
