|
| 1 | +# Type Conversions |
| 2 | + |
| 3 | +Most of the time, operators and functions automatically convert the values given to them to the right type. |
| 4 | + |
| 5 | +For example, |
| 6 | + |
| 7 | +- **alert automatically converts any value to a string to show it**. |
| 8 | +- **Mathematical operations convert values to numbers.** |
| 9 | + |
| 10 | +# String Conversion |
| 11 | + |
| 12 | +call the String(value) function to convert a value to a string: |
| 13 | + |
| 14 | +```javascript |
| 15 | +let value = true; |
| 16 | +alert(typeof value); // boolean |
| 17 | + |
| 18 | +value = String(value); // now value is a string "true" |
| 19 | +alert(typeof value); // string |
| 20 | + |
| 21 | +String(false); // "false" |
| 22 | +String(null); // "null" |
| 23 | +``` |
| 24 | + |
| 25 | +# Numeric Conversion |
| 26 | + |
| 27 | +- Numeric conversion happens in **mathematical functions** and **expressions** automatically. |
| 28 | +- We can use the Number(value) function to explicitly convert a value to a number: |
| 29 | + |
| 30 | +```javascript |
| 31 | +Number(''); //0 |
| 32 | +Number(' '); //0 |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +# Boolean conversion rules |
| 38 | + |
| 39 | +Please note: the string with zero "0" is true |
| 40 | + |
| 41 | +```javascript |
| 42 | +alert(Boolean('0')); // true |
| 43 | +alert(Boolean(' ')); // spaces, also true (any non-empty string is true) |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +# Precedence of operator |
| 49 | + |
| 50 | +if you use same type of operators in an expression (i.e. expression contains numbers) then it will perform left to right operation |
| 51 | + |
| 52 | +```javascript |
| 53 | +alert(5 + 5 + 'px'); // 10px |
| 54 | +alert('px' + 5 + 5); // px55 |
| 55 | +``` |
| 56 | + |
| 57 | +if you have higher Precedence operators in expression the evaluation will be different |
| 58 | + |
| 59 | +```javascript |
| 60 | +alert('px' + 5 * 5); // px25 |
| 61 | +``` |
| 62 | + |
| 63 | +For more on this [read here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) |
| 64 | + |
| 65 | +# Some special functions |
| 66 | + |
| 67 | +## parseFloat() vs parseInt vs Number vs +"(string value)" |
| 68 | + |
| 69 | +parseFloat/parseInt is for parsing a string, while Number/+ is for coercing a value to a number. They behave differently. But first let's look at where they behave the same: |
| 70 | + |
| 71 | +```javascript |
| 72 | +parseFloat('3'); // => 3 |
| 73 | +Number('3'); // => 3 |
| 74 | +parseFloat('1.501'); // => 1.501 |
| 75 | +Number('1.501'); // => 1.501 |
| 76 | +parseFloat('1e10'); // => 10000000000 |
| 77 | +Number('1e10'); // => 10000000000 |
| 78 | +``` |
| 79 | + |
| 80 | +parseInt will trucate decimal part |
| 81 | + |
| 82 | +```javascript |
| 83 | +parseInt('12.2'); // 12 |
| 84 | +``` |
| 85 | + |
| 86 | +However, if your input starts with a number and then contains other characters, parseFloat truncates the number out of the string, while Number gives NaN (not a number): |
| 87 | + |
| 88 | +```javascript |
| 89 | +parseFloat('1x'); // => 1 |
| 90 | +parseInt('1x'); // => 1 |
| 91 | +parseFloat('3px'); // => 3 |
| 92 | +parseInt('3px'); // => 3 |
| 93 | +Number('1x'); // => NaN |
| 94 | +``` |
| 95 | + |
| 96 | +Number understands hexadecimal input while parseFloat does not: |
| 97 | + |
| 98 | +```javascript |
| 99 | +parseFloat('0x10'); // => 0 |
| 100 | +parseInt('0x10'); // => 16 |
| 101 | +Number('0x10'); // => 16 |
| 102 | +``` |
| 103 | + |
| 104 | +Note that using the unary + operator is exactly the same as using Number as a function: |
| 105 | + |
| 106 | +```javascript |
| 107 | +Number('0x10'); // => 16 |
| 108 | ++'0x10'; // => 16 |
| 109 | +Number('10x'); // => NaN |
| 110 | ++'10x'; // => NaN |
| 111 | +Number('40'); // => 40 |
| 112 | ++'40'; // => 40 |
| 113 | +``` |
| 114 | + |
| 115 | +For empty string, they are different. |
| 116 | + |
| 117 | +- **+"" and Number("") returns 0** |
| 118 | +- **parseFloat("") and parseInt("") returns NaN** |
| 119 | + |
| 120 | +```javascript |
| 121 | +parseFloat(''); // => NaN |
| 122 | +Number(''); // => 0 |
| 123 | +parseFloat(' \r\n\t'); // => NaN |
| 124 | +Number(' \r\n\t'); // => 0 |
| 125 | +``` |
| 126 | + |
| 127 | +## toString() vs String() |
| 128 | + |
| 129 | +value.toString() will cause an error if value is `null` or `undefined`. String(value) should not. |
| 130 | + |
| 131 | +```javascript |
| 132 | +var value = null; |
| 133 | +alert(value.toString()); // TypeError exception |
| 134 | + |
| 135 | +var value = null; |
| 136 | +alert(String(value)); // "null" |
| 137 | +``` |
| 138 | + |
| 139 | +# object to primitive |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +for more [read here](https://javascript.info/object-toprimitive#tostring-valueof) |
| 144 | + |
| 145 | +// TODO Remaining notes here |
0 commit comments