0%

javascript tricks

JavaScript Tricks

1. typeof null === 'object'. This is a known bug in JavaScript.

2. typeof NaN === 'number'. But, this is true.

3. ... perform a shallow copy(only the first level is copied by value, other levels is copied by reference).

1
2
3
4
5
6
7
8
const arr = [1, 2, [3, 4]];
const newArr = [...arr];

newArr[1] = 3;
console.log(arr); // [1, 2, [3, 4]]

newArr[2][0] = 5;
console.log(arr); // [1, 2, [5, 4]]

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); // 3
console.log(Math.floor(num)); // 3

8. !! can be used to convert a value to a boolean.

1
2
3
const a = 1;
console.log(!!a); // true
console.log(!!0); // false

9. How to quit node.js REPL?

1
> .exit
1
> process.exit()