- Type coercion is the process of converting value from one type to another example: string to number, or number to string.
- Since javascript is a weakly typed language type coercion can be done to values implicitly or explicitly
When we convert value from type to another explicitly it is called type casting example: Number('10')
this is a way of converting value from string
to number
explicitly
Implicit type conversion can happen when we apply some operators on values, example: !10
here value 10 is converted from number to boolean implicitly with type coercion because of logical operator NOT
that we added before the number
- to string: Explicitly using
String(value)
or implicitly usingvalue + ''
- to boolean: Explicitly using
Boolean(value)
or implicitly using logical operators(|| && !)
- to number: Explicitly using
Number(value)
or implicitly using more operators
conversion logic for primitives and objects works differently but both can be converted to any of the three types mentioned above
We can convert any primitive type to string explicitly using String()
and we can do it implicitly using the +
operator with a string
Conversion to string
// Explicit conversion to string
String(123) // '123'
String(-12.3) // '-12.3'
String(null) // 'null'
String(undefined) // 'undefined'
String(true) // 'true'
String(false) // 'false'
// Implicit conversion to string
123 + '' // '123'
We can convert any primitive to boolean type explicitly using Boolean()
and we can do it implicitly using logical operators (|| && !)
Conversion to boolean
// Explicit conversion to string
Boolean(2) // true
// Implicit conversion to string
if (2) { } // if condition will return true
!2 // false
2 || 'hello' // this will return 2
||
and &&
operators will do implicit conversion internally but it will return the operand value
We can convert any primitive to number type explicitly using Number()
and implicitly using the following operators
- Comparison operators
>
<
<=
>=
- Bitwise operators
|
&
^
~
- Arithmetic operators
-
+
*
/
%
the+
will not make number conversion if any side is string - Unary
+
operator - Loose equality operator
==
only when both sides are not strings and they are notnull
orundefined
Number('123') // explicit
+'123' // implicit
123 != '456' // implicit
4 > '5' // implicit
5/null // implicit
true | 0 // implicit
Number(null) // 0
Number(undefined) // NaN
Number(true) // 1
Number(false) // 0
null == false // false -> because the false is coerced to 0 so the comaprison is null == 0
undefined == false // false -> because the false is coerced to 0 so the comparison is undefined == 0
"1" == true // true -> because the true is coerced to 1 and then the "1" is coerced to 1 so the comparison is 1 == 1
"0" == false // true -> because the false is coerced to 0 and then the "0" is coerced to 0 so the comparison is 0 == 0
3 + "1" // "31" -> 3 will be coerced to "3"
3 + +"1" // 4 -> the right unary + will convert "1" to 1 and it will be 3 + 1 = 4
3 + true // 4 -> true is coerced to 1 so it will be 3 + 1
3 + false // 3 -> false is coerced to 0
true + false // 1 -> true is coerced to 1 and false is coerced to 0