Skip to content

Matthias Böhm

JavaScript Type Conversion & Coercion - code desk wall
  • JavaScript
  • Fundamentals

JavaScript Type Conversion & Coercion

Understanding one of JavaScript's biggest sources of confusion

JavaScript can be confusing sometimes and one of the biggest sources of confusion is type conversion and coercion in JavaScript.

Here is an example:

'5' + '3'
-> '53'

'5' - '3'
-> 2

Why the hell is '5' + '3' = '53' but '5' - '3' = 2? Coercion is the answer and we are going to uncover what means and why it happens.

What is type conversion and coercion in JavaScript?

Let's start first by explaining what type conversion and type coercion menus in JavaScript.

Type conversion - sometimes also called type casting - is the process of converting a value from one type to another.

It is explicit and a process we trigger manually.

For example, converting a String with the value'32' to a the value Number 32.

This is an essential task for a developer and you will use this all the time.

Type coercion however occurs when a value is automatically converted to another type.

It happens implicitly and is triggered automatically by JavaScript.

An example would be:

console.log('2' + 3);
-> 23

Here the String with the value '2' is automatically converted to the type Number.

Explicit type conversion in JavaScript

Let's start with exploring explicit type conversion in JavaScript and how you can trigger it manually.

Type conversion to String

If you want to convert a value explicitly to string you can either use the String() constructor function.

String(34) // Number
String(true) // Boolean
String(false) // Boolean
String(2.32) // Number / Float
String(null) // null
String(undefined) // undefined
String(NaN) // NaN / Number

Or you can use the toString() method, which is available on every object (descended from Object).

The toString() method is also called every time an Object is converted to String and you can overwrite it if you want to.

Number(12).toString(); // Number
BigInt(55).toString() // BigInt
Boolean(false).toString() // Boolean
Symbol('xy').toString() // Symbol
Object().toString() // Object
'[object Object]'
({}).toString() // Object literal {}
'[object Object]'

Type conversion to Number

To convert a value into a number, you can use the Number() constructor function.

Number('test') // String
Number('234') // String
Number('') // String
Number(false) // Boolean (false)
Number(true) // Boolean (true)
Number(null) // null
Number(undefined) // undefined

You can also use the parseInt() function to convert Strings to Numbers. The first argument is the string and the second one the radix (10 by default).

parseInt(11100) // Radix 10 by default
parseInt('11100', 2) // Radix 2 -> binary

And there is the parseFloat() method, which converts Strings into a floating point Number.


Type conversion to Boolean

As you might have guessed, we can use the Boolean() constructor function to convert a value into Boolean.

The way conversion to Boolean in JavaScript works is actually pretty straightforward.

Falsy values will be converted to false and truthy values to true.

Falsy values are null, undefined, NaN, 0, ''. They convert to false.

The rest are truthy values and they convert to true.

Boolean(null) // falsy
Boolean(undefined) // falsy
Boolean(0) // falsy
Boolean('') // falsy
Boolean(NaN) // falsy
Boolean(123) // truthy
Boolean('test') // truthy
Boolean(' ') // truthy
Boolean(2.34) // truthy
Boolean({}) // truthy
Boolean([]) // truthy

Implicit (automatic) type conversion in JavaScript

Now let's talk about implicit or automatic type conversion in JavaScript - or better known as coercion.

Implicit type conversion to String

Whenever the + operator is used in JavaScript when any operand is a String, the implicit type coercion in JavaScript is triggered and the other operands are converted to String.

'hi' + 3
'hi' + true
'hi' + undefined
'hi' + null
'hi' + {}
'hi[object Object]'

Implicit type conversion to Number

Implicit conversion to the Number type happens when an arithmetic operator like -, +, *, /, % is used.

It also happens when comparison operators are used like <, >, >=, <= are used and the unary + operator is used.

3 + true
3 - '1'
12 / `3`
3 * false
12 % true

If one of the operands is NaN or the result of the implicit coercion is NaN, the result will be NaN.

12 / 'test'
3 * {}

Implicit type conversion to Boolean

Implicit conversion of a value to Boolean happens when logical operators &&, || or ! are used, or in logical context.

The logical operators convert the values to Boolean for the comparison but still return the actual values (as non Boolean values).

0 || 1 // 0 is falsy & 1 truthy, so 1 gets returned
1 || 2 // 1 is truthy so 1 gets returned
if (5) { // implicit Boolean conversion

📢 Never miss an article

  • ✔️ Receive in-depth insights for web developers and software engineers
  • ✔️ Learn JavaScript and web development the proper way
  • ✔️ No spam. Unsubscribe at any time

Join the newsletter and power up your web dev game ☝️

Profile picture Matthias Böhm round
·4 min read