DEV Community

Discussion on: JavaScript Interview Question #17: Sum of Two Empty Arrays

Collapse
 
willsmart profile image
willsmart • Edited

Fun fact: any combo of any number of [], 0, "", whitespace (mix-and-match is ok: " \n\n\n\r \t"), ".", "0" (repeated if you like: "000"), null, false added together will loose equal false, so long as:

  • you're careful never to stringify a false or null,
  • or put whitespace between two zeros or a dot and a zero
  • and if you have a dot, there is only one and there must also be at least one zero
  • other rules I haven't thought of
0+"0"+[]+" \n\t\r  " == false // yep, it's "00 \n\t\r  "
false+0+[]+"00000"+[]+"   " == false // yep, it's "000000   "
0000+null+[]+[]+0+'0'+""+[] == false, // totally, "000"
0+false+null+""+[]+[]+'000'+0 + '.' == false // uhhuh, "00000."

but

[]+[]+""+false != false // the false was concatted as "false". "false" isn't falsey.
Enter fullscreen mode Exit fullscreen mode

On a related note, static typing is great 😁 and there are plenty of tools/libs/langs available to code JS without this JS-framer weirdness.

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Awesome comment! Thanks for the additional info.

I think JS is like poker to some extent. It takes a minute to learn and a lifetime to master 😂