"Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring."
{} instanceof null
// Uncaught TypeError: Right-hand side of 'instanceof' is not an objecttypeof null
// 'object'null is not an object, but a primitive value. typeof null returning 'object' is a bug, which is not fixed due to backward-compatability.
'2' + '2' - '2'
// 20+is both concatenation and addition ('2' + '2'is'22');-is only subtraction and arguments are coerced to numbers ('22' - 2is20).
console.log(abc)
// Uncaught ReferenceError: abc is not definedconsole.log(typeof abc)
// 'undefined'Explanation
typeof operator can check whether the variable has been declared.
console.log(Array(5))
// [ <5 empty items> ]
const arr = [1,,,2,,,3]
console.log(arr.length)
// 7
arr.forEach((item, idx) => console.log(item, idx))
// 1 0
// 2 3
// 3 6
console.log(JSON.stringify(arr))
// '[1,null,null,2,null,null,3]'
const arr2 = [...arr]
arr2.forEach((item, idx) => console.log(item, idx))
// 1 0
// undefined 1
// undefined 2
// 2 3
// undefined 4
// undefined 5
// 3 6
arr[1000] = 1
console.log(arr.length)
// 1001
console.log(arr)
// [ 1, <2 empty items>, 2, <2 empty items>, 3, <993 empty items>, 1 ]Story
Someday I was writing a Telegram bot and spent several hours debugging one issue.
Bot had logging functionality and logged something JSON.stringified, and after some changes I got an error:
FATAL ERROR: JS Allocation failed - process out of memory
In short, the problem was that I was setting some array's item by Telegram user's ID (which was big like 987654321). So JS tried to stringify an array with nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one null and ran out of memory:
arr[user.id] = 'hi'
JSON.stringify(arr)
// FATAL ERROR: JS Allocation failed - process out of memoryparseInt(0.5)
// 0
parseInt(0.005)
// 0
parseInt(0.0000005)
// 5Explanation
- The
parseIntfunction converts its first argument to a string, parses that string, then returns an integer orNaN. - If
parseIntencounters a character that is not a numeral in the specifiedradix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. (0.5).toString()->'0.5'(0.0000005).toString()->'5e-7'
018 === 022
// trueExplanation
- A leading
0on an integer literal, or a leading0o(or0O) indicates it is in octal. Octal integer literals can include only the digits0β7. (MDN Web Docs) 022starts with0=> it's in octal018contains8=> it's not in octal, but in decimal22in octal =18in decimal