Type Conversion
Understand JavaScript type conversion and coercion, Number, String, Boolean, parseInt and the strange results of implicit conversion, with examples.
On this page
1. What is type conversion?
It happens in two ways:
| Kind | Who does it | Example |
|---|---|---|
| Explicit conversion | You, on purpose | Number("42") |
| Implicit conversion (coercion) | JavaScript itself, during an operation | "5" * 2 |
2. Why does it matter?
Input fields, URL params and localStorage always give you a string. If you add them thinking they are numbers, the result of "10" + "5" is "105", not 15.
| Relying on coercion | Explicit conversion |
|---|---|
| You have to guess the result | The result is clear and predictable |
| Adding prices gives text like 105 | Convert to a number first, get the right total |
| Bugs stay hidden | Check for NaN and know right away |
| The reader gets confused | The intent is written in the code |
3. Where is it used?
| Situation | Conversion |
|---|---|
| Age or price from a form input | Number(input.value) |
| Page number from the URL | Number(params.get("page")) |
| 16 from the CSS value "16px" | parseInt("16px", 10) |
| Showing a number as text | String(total) or a template literal |
| Checking whether a value exists | Boolean(value) |
4. How does it work?
JavaScript does only three conversions: to string, to number, to boolean.
Some rules for number conversion:
| Value | Number(value) |
|---|---|
"42" | 42 |
" 12 " | 12 (spaces ignored) |
"" | 0 |
"12px" | NaN |
true / false | 1 / 0 |
null | 0 |
undefined | NaN |
5. Code example
Explicit conversion:
const ageInput = "25";
const age = Number(ageInput);
console.log(age + 1);
console.log(String(99));
console.log(Boolean("hello"));
console.log(Boolean(""));Output: 26, "99", true, false.
Number() vs parseInt() / parseFloat():
console.log(Number("12px"));
console.log(parseInt("12px", 10));
console.log(parseFloat("3.75kg"));
console.log(Number.isNaN(Number("abc")));Output: NaN, 12, 3.75, true.
Results of implicit coercion:
console.log("5" + 1);
console.log("5" - 1);
console.log("4" * "2");
console.log(true + 1);
console.log(null + 1);
console.log(undefined + 1);Output: "51", 4, 8, 2, 1, NaN.
How does the code work?
| Item | Meaning |
|---|---|
Number("25") | Gives a number if the whole string is a valid number |
parseInt("12px", 10) | Reads as much number as it finds from the start, in base 10 |
parseFloat | For numbers with decimals |
"5" + 1 | It sees a string, so it joins: "51" |
"5" - 1 | Minus works only on numbers, so "5" became a number |
Number.isNaN() | The right way to check whether a value is NaN |
Interview tip
Why is [] + [] equal to "" (an empty string) and [] + {} equal to "[object Object]"? The plus operator first turns both objects into primitives (strings). An empty array becomes an empty string and an object becomes "[object Object]". This question tests your understanding of coercion.
6. Common mistakes
Mistake: Adding an input value directly
Why? input.value is always a string, so adding 10 and 5 gives 105.
Better: Convert first with Number() or input.valueAsNumber, then do the calculation.
Mistake: Writing parseInt without a radix
Why? In some formats the base can be guessed wrong, and the intent of the code is not clear.
Better: Always write parseInt(value, 10), or use Number() for a whole string.
Mistake: Checking NaN with value === NaN
Why? NaN is not equal to anything, not even itself, so this check always gives false.
Better: Use Number.isNaN(value).
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Data Types |
| After this | Operators, Truthy, Falsy & Equality |
| Related | Numbers & Math, Forms & Validation |
Final quick revision
| Point | Detail |
|---|---|
| Explicit | Number(), String(), Boolean() |
| Implicit | JavaScript converts by itself during operators |
+ and a string | Always joins strings |
-, *, / | Try to make numbers |
parseInt | Reads the leading number, pass radix 10 |
NaN check | Number.isNaN() |