JavaScript Tricks
1. typeof null === 'object'
. This is a known bug in JavaScript.
2. typeof NaN === 'number'
. But, this is true.
1 2 3 4 5 6 7 8
| const arr = [1, 2, [3, 4]]; const newArr = [...arr];
newArr[1] = 3; console.log(arr);
newArr[2][0] = 5; console.log(arr);
|
4. There is no pass by reference in JavaScript, only pass by value.
5. In JavaScript, everything is object - this is not true.
6. type print
or window.print
in Browser console
, you will get a print dialog.
7. |0
can be used to remove the fractional part of a number.
It has the same effect as Math.floor()
.
1 2 3
| const num = 3.14; console.log(num | 0); console.log(Math.floor(num));
|
8. !!
can be used to convert a value to a boolean.
1 2 3
| const a = 1; console.log(!!a); console.log(!!0);
|
9. How to quit node.js REPL?