Skip to content
BasicsChapter 6 / 47Beginner4 min read

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:

KindWho does itExample
Explicit conversionYou, on purposeNumber("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 coercionExplicit conversion
You have to guess the resultThe result is clear and predictable
Adding prices gives text like 105Convert to a number first, get the right total
Bugs stay hiddenCheck for NaN and know right away
The reader gets confusedThe intent is written in the code

3. Where is it used?

SituationConversion
Age or price from a form inputNumber(input.value)
Page number from the URLNumber(params.get("page"))
16 from the CSS value "16px"parseInt("16px", 10)
Showing a number as textString(total) or a template literal
Checking whether a value existsBoolean(value)

4. How does it work?

JavaScript does only three conversions: to string, to number, to boolean.

Coercion based on the operator
See the operation
Plus and one side is a string, join as strings
Minus, multiply, divide, turn into numbers
if or logical operators, turn into a boolean

Some rules for number conversion:

ValueNumber(value)
"42"42
" 12 "12 (spaces ignored)
""0
"12px"NaN
true / false1 / 0
null0
undefinedNaN

5. Code example

Explicit conversion:

js
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():

js
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:

js
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?

ItemMeaning
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
parseFloatFor numbers with decimals
"5" + 1It sees a string, so it joins: "51"
"5" - 1Minus 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

Final quick revision

PointDetail
ExplicitNumber(), String(), Boolean()
ImplicitJavaScript converts by itself during operators
+ and a stringAlways joins strings
-, *, /Try to make numbers
parseIntReads the leading number, pass radix 10
NaN checkNumber.isNaN()