|
| 1 | +## Methods of primitives |
| 2 | + |
| 3 | +- javascript allows to work with primitives as though they were objects (provide methods to call) |
| 4 | +- this is allowed via object wrapper, to provide method access on primitives an object wrapper that provides the method is created and then destroyed. |
| 5 | +- some object wrappers are `String`, `Number`, `Boolean`, `Symbol` |
| 6 | +- the above provide special methods to the primitives |
| 7 | + |
| 8 | +```javascript |
| 9 | +let str = 'Hello'; |
| 10 | +console.log(str.toUpperCase()); |
| 11 | +``` |
| 12 | + |
| 13 | +- `str` is a primitive string, so the moment of accessing `toUpperCase` a special object is created that knows the value of `str` and has methods like `toUpperCase`, the method runs and return the result and is destroyed, leaving the primitive `str` |
| 14 | + |
| 15 | +### Numbers |
| 16 | + |
| 17 | +- regular number(double precision floating point numbers) and bigInt(needed when regular number exceed 2^53, -2^53) |
| 18 | +- in js we can shorten the number by appending `e` to the number and specifying 0 |
| 19 | + |
| 20 | +```javascript |
| 21 | +let billion = 1e9; // 1 billion 1 and 9 zeros |
| 22 | +console.log(7.3e9); // 7,300,000,000 // 7.3 * 1000000000 |
| 23 | +let ms = 1e-6; // 0.000001 (1/1000000) |
| 24 | +``` |
| 25 | + |
| 26 | +- `toString(base)`, return string representation of num in given base |
| 27 | + |
| 28 | +```javascript |
| 29 | +let num = 255; |
| 30 | +console.log(num.toString(16)); //ff |
| 31 | +console.log(num.toString(2)); //11111111 |
| 32 | + |
| 33 | +console.log((123456).toString(36)); // 2n9c two dots not a typo the first dot is considered a decimal point, this is how you call a methods directly on a number |
| 34 | +``` |
| 35 | + |
| 36 | +- Rounding `Math.floor`(3.1->3, -1.1 -> -2), `Math.ceil`(3.1->4, -1.1 -> -1), `Math.round`(3.1->3, 3.6-> 4, -1.1->-1), `Math.trunc`(remove the decimal part, no support in IE) |
| 37 | +- round of to n place using `toFixed(n)`, returns string |
| 38 | + |
| 39 | +- imprecise calculations |
| 40 | + |
| 41 | +```javascript |
| 42 | +console.log(1e500); // Infinity |
| 43 | +console.log(0.1 + 0.2 == 0.3); // false 0.1 and 0.2 are unending expression when converted to binary |
| 44 | +// solution |
| 45 | +console.log(+(0.1 + 0.2).toFixed(2)); // 0.3 |
| 46 | +``` |
| 47 | + |
| 48 | +- because of internal representation of number there exists two 0 (0, -0) this is because of the sign bit |
| 49 | +- Tests, inFinite and isNaN |
| 50 | +- iSNaN converts argument to number and then test wether that number is NaN |
| 51 | +- isFinite converts argument to number and returns true is number is regular and not any of NaN/Infinity/-Infinity |
| 52 | + |
| 53 | +```javascript |
| 54 | +// Infinity(greater) and -Infinity(less) than any other value |
| 55 | +// |
| 56 | +// NaN === NaN // false |
| 57 | +``` |
| 58 | + |
| 59 | +- `Object.is` compares like `===` but more reliabel for edge cases `Object.is(NaN, NaN)=== true // true`, also value `Object.is(0,-0)=== false// true`, ie (0,-0) are treated differently `in Object.is` |
| 60 | +- parseInt and parseFloat, read number from a string until they cant, the first read should be a number though, otherwise return a NaN |
| 61 | +- both parseInt and parseFloat have a second radix argument, this specifies a base |
| 62 | +- `Math` object |
| 63 | + |
| 64 | +```javascript |
| 65 | +Math.random(), returns number between 0 and 1 not including 1 |
| 66 | +Math.max(a,b,c) |
| 67 | +Math.min(a,b,c) |
| 68 | +Math.pow(2,10) //1024 |
| 69 | +``` |
0 commit comments