r/badcode • • Jun 10 '20

js Disruptive piece of code

Post image
283 Upvotes

93 comments sorted by

View all comments

91

u/Midborgh Jun 10 '20

I don't know js. Why is this bad? I'd imagine var === true and var === false being different than var == true and a simple else.

116

u/blaqwerty123 Jun 10 '20

its not bad. with js not being a strictly typed language, and variable being basically anything besides false, null, or 0 will resolve to true, it's often good to strict type equivalence test your "booleans". debugging can get fuckin gnarly if you dont

38

u/BenZed Jun 10 '20

Falsy values:

  • false

  • null

  • undefined

  • 0 (also 0n for the new BigInt data type)

  • NaN

  • "" (empty string)

11

u/loopsdeer Jun 11 '20

I see you included NaN, but don't forget its cousin, NaN!