Skip to content

Latest commit

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

"Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring."


subjective null

{}instanceofnull// TypeError: Right-hand side of 'instanceof' is not an object
typeofnull// 'object'

Explanation

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

'2'+'2'-'2'// 20

Explanation

  • + is both concatenation and addition ('2' + '2' is '22');
  • - is only subtraction and arguments are coerced to numbers ('22' - 2 is 20).

typeof undeclared

console.log(abc)// ReferenceError: abc is not defined
console.log(typeofabc)// 'undefined'

Explanation

typeof operator can check whether the variable has been declared.


console.log(Array(5))// [ <5 empty items> ]constarr=[1,,,2,,,3]console.log(arr.length)// 7arr.forEach((item,idx)=>console.log(item,idx))// 1 0// 2 3// 3 6console.log(JSON.stringify(arr))// '[1,null,null,2,null,null,3]'constarr2=[...arr]arr2.forEach((item,idx)=>console.log(item,idx))// 1 0// undefined 1// undefined 2// 2 3// undefined 4// undefined 5// 3 6arr[1000]=1console.log(arr.length)// 1001console.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 memory

parseInt

parseInt(0.5)// 0parseInt(0.005)// 0parseInt(0.0000005)// 5

Explanation

  • The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN.
  • If parseInt encounters a character that is not a numeral in the specified radix, 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

018===022// true

Explanation

  • A leading 0 on an integer literal, or a leading 0o (or 0O) indicates it is in octal. Octal integer literals can include only the digits 0 – 7. (MDN Web Docs)
  • 022 starts with 0 => it's in octal
  • 018 contains 8 => it's not in octal, but in decimal
  • 22 in octal = 18 in decimal

letx='hi'x.lang// undefinedx.lang='eng'// 'eng'x.lang// undefined'lang'inx// TypeError: Cannot use 'in' operator to search for 'lang' in hi

About

I Love JavaScript πŸ’›

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Contributors