|
| 1 | +##Type Coercion: |
| 2 | + |
| 3 | +* Type coercion is the process of converting value from one type to another example: string to number, or number to string. |
| 4 | +* Since javascript is a weakly typed language type coercion can be done to values implicitly or explicitly |
| 5 | + |
| 6 | +###Explicit type conversion |
| 7 | + |
| 8 | +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 |
| 9 | + |
| 10 | +###Implicit type conversion(type coercion): |
| 11 | + |
| 12 | +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 |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +###Types of conversion |
| 17 | + |
| 18 | +1. to string: Explicitly using`String(value)` or implicitly using`value + ''` |
| 19 | +2. to boolean: Explicitly using`Boolean(value)` or implicitly using logical operators`(|| && !)` |
| 20 | +3. to number: Explicitly using`Number(value)` or implicitly using more operators |
| 21 | + |
| 22 | +####Important note: |
| 23 | +conversion logic for primitives and objects works differently but both can be converted to any of the three types mentioned above |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +##Conversion to string: |
| 28 | + |
| 29 | +We can convert any primitive type to string explicitly using`String()` and we can do it implicitly using the`+` operator with a string |
| 30 | + |
| 31 | + |
| 32 | +####Example 19.0: |
| 33 | + |
| 34 | +Conversion to string |
| 35 | + |
| 36 | +```javascript |
| 37 | +// Explicit conversion to string |
| 38 | +String(123)// '123' |
| 39 | +String(-12.3)// '-12.3' |
| 40 | +String(null)// 'null' |
| 41 | +String(undefined)// 'undefined' |
| 42 | +String(true)// 'true' |
| 43 | +String(false)// 'false' |
| 44 | + |
| 45 | +// Implicit conversion to string |
| 46 | +123+''// '123' |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +##Conversion to boolean: |
| 52 | + |
| 53 | +We can convert any primitive to boolean type explicitly using`Boolean()` and we can do it implicitly using logical operators`(|| && !)` |
| 54 | + |
| 55 | +####Example 19.1: |
| 56 | + |
| 57 | +Conversion to boolean |
| 58 | + |
| 59 | +```javascript |
| 60 | +// Explicit conversion to string |
| 61 | +Boolean(2)// true |
| 62 | + |
| 63 | +// Implicit conversion to string |
| 64 | +if (2) { }// if condition will return true |
| 65 | +!2// false |
| 66 | +2||'hello'// this will return 2 |
| 67 | +``` |
| 68 | + |
| 69 | +##Boolean implicit conversion will return operand value: |
| 70 | + |
| 71 | +`||` and`&&` operators will do implicit conversion internally but it will return the operand value |
| 72 | + |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +##Conversion to number: |
| 77 | + |
| 78 | +We can convert any primitive to number type explicitly using`Number()` and implicitly using the following operators |
| 79 | + |
| 80 | +1. Comparison operators`>``<``<=``>=` |
| 81 | +2. Bitwise operators`|``&``^``~` |
| 82 | +3. Arithmetic operators`-``+``*``/``%` the`+` will not make number conversion if any side is string |
| 83 | +4. Unary`+` operator |
| 84 | +5. Loose equality operator`==` only when both sides are not strings and they are not`null` or`undefined` |
| 85 | + |
| 86 | +```javascript |
| 87 | +Number('123')// explicit |
| 88 | ++'123'// implicit |
| 89 | +123!='456'// implicit |
| 90 | +4>'5'// implicit |
| 91 | +5/null// implicit |
| 92 | +true|0// implicit |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +##Some conversion values |
| 98 | + |
| 99 | +```javascript |
| 100 | +Number(null)// 0 |
| 101 | +Number(undefined)// NaN |
| 102 | +Number(true)// 1 |
| 103 | +Number(false)// 0 |
| 104 | +null==false// false -> because the false is coerced to 0 so the comaprison is null == 0 |
| 105 | +undefined==false// false -> because the false is coerced to 0 so the comparison is undefined == 0 |
| 106 | +"1"==true// true -> because the true is coerced to 1 and then the "1" is coerced to 1 so the comparison is 1 == 1 |
| 107 | +"0"==false// true -> because the false is coerced to 0 and then the "0" is coerced to 0 so the comparison is 0 == 0 |
| 108 | +3+"1"// "31" -> 3 will be coerced to "3" |
| 109 | +3++"1"// 4 -> the right unary + will convert "1" to 1 and it will be 3 + 1 = 4 |
| 110 | +3+true// 4 -> true is coerced to 1 so it will be 3 + 1 |
| 111 | +3+false// 3 -> false is coerced to 0 |
| 112 | +true+false// 1 -> true is coerced to 1 and false is coerced to 0 |
| 113 | + |
| 114 | +``` |