Type coercion

🏷️ Javascript

Type coercion is a process converting value from one type to another

There are 2 types of coercion: explicit and implicit

Explicit: Number(value)

Implicit: 1 == null or by surrounding context like if (value) {}

Strict equality operator (===) doesn’t trigger implicit coercion

String conversion

Explicit: Use String(value)

Implicit: Use + operator.

value + "";

Symbol an only be explicitly converted. If implicitly converting it, TypeError is thrown

Boolean conversion

Explicit: Boolean(value)

Implicit: in logical context or logical operators (||, &&, !)

|| and && convert values internally but return the original value instead boolean value

Falsy value

Number conversion

Explicit: Number(value)

Implicit:

When converting a string to number, Javascript will trim whitespaces. The result will be NaN if the string is an invalid number, 0 if empty.

null -> 0

undefined -> NaN

Symbol can’t be converted explicitly or implicitly. TypeError will be thrown

Special cases

Type coersion for objects

Objects -> primitives by [[ToPrimitive]] method

Boolean conversion always returns true for non-primitive value

Numeric and string conversion use 2 methods of the input object: valueOf() and toString()

Steps:

String conversion call toString() first, then fallback to valueOf()

Number conversion call valueOf(), and fallback to toString() == and binary + trigger number conversion by default


https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839