Skip to content
DevMeme
3741 of 7590
Languages Post #4081 · source on Telegram

JavaScript's Schrödinger's Boolean: Both True and False

Description

A two-part meme highlighting a notorious JavaScript language quirk. The top half is a screenshot of a browser's JavaScript console. The first command, 'new Boolean(false)', correctly returns a Boolean object with a value of {false}. The second command, 'new Boolean(new Boolean(false))', counter-intuitively returns a Boolean object with a value of {true}. The bottom half of the image uses the 'Skyrim 100' skill meme format, showing the word 'Javascript' with the number '100' next to it, implying mastery or a high level of absurdity. The humor is rooted in JavaScript's type coercion rules: while 'new Boolean(false)' creates an object with a false value, the object itself is 'truthy'. Therefore, passing this truthy object to another Boolean constructor results in a new Boolean object with a value of true. This is a classic 'wat' moment for developers and a textbook example of why using the Boolean object constructor is discouraged in favor of primitive boolean values

Comments

47
Anonymous ★ Top Pick This is why the senior dev on your team insists that `if (!!someObject)` is the only way to be sure. They've been hurt before
  1. Anonymous ★ Top Pick

    This is why the senior dev on your team insists that `if (!!someObject)` is the only way to be sure. They've been hurt before

  2. Anonymous

    JavaScript: where one extra heap allocation can promote even false to middle management - proof that with enough abstraction, reality is negotiable

  3. Anonymous

    This is the same JavaScript quirk that made us add "5+ years experience with TypeScript" to job postings - not for the type safety, but to filter out developers who still think new Boolean(false) is a good idea after their third production outage

  4. Anonymous

    In JavaScript, even false becomes true once you give it an object - same career path as most middle managers

  5. Anonymous

    Ah yes, the classic JavaScript interview question that separates those who've been burned from those about to be. When you use `new Boolean(false)`, JavaScript helpfully creates an object wrapper - because apparently primitive booleans weren't confusing enough. Since all objects are truthy (even ones wrapping false), your carefully crafted `if (!myBool)` check suddenly fails in spectacular fashion. It's like JavaScript looked at C++'s operator overloading footguns and said 'hold my beer.' This is why ESLint has a rule against using Boolean/Number/String constructors with `new`, and why senior devs instinctively reach for `Boolean(value)` or `!!value` instead. Remember: in JavaScript, `new Boolean(false) == true` is false, but `new Boolean(false) ? true : false` is true. Because consistency is overrated when you can have *flexibility*

  6. Anonymous

    JavaScript is the only stack where a heap allocation turns false into true - thanks to ToBoolean’s “objects are truthy” rule, which is why eslint ships no-new-wrappers

  7. Anonymous

    new Boolean(false) is truthy because in JS, mere existence outweighs actual value - the ultimate architectural pattern for legacy tolerance

  8. Anonymous

    JavaScript in one line: box false, get an object; box the object, ToBoolean says true - autoboxing, the only promotion where false gets a raise

  9. dev_meme 4y

    nonnull pointer -> true (at least in c++)

    1. @RiedleroD 4y

      bool(False) → False (in python)

      1. dev_meme 4y

        seems logical

  10. @bezuhten 4y

    seems legit

  11. @ShiningFlames 4y

    Don't see any problem here :/

    1. @callofvoid0 4y

      JS fan 100

      1. @azizhakberdiev 4y

        IQ level - javascript

        1. Deleted Account 4y

          Hey hey

  12. @VladislavSmolyanoy 4y

    It creates new instance → returns true because creating was successful I guess?

    1. @saidov 4y

      +

    2. @mozilaip 4y

      Who cares? JavaScript bad, SHIT, LoL funny meme 00))))0

  13. @maggelia 4y

    Yea that could be one of the only right things in JS, it's just telling you that indeed he did create it

  14. @Valeron933 4y

    new operator probably returns non-zero value

  15. @SrZorro 4y

    typeof new Boolean(true) is an object, and an object is true when casted to boolean

    1. dev_meme 4y

      what about typeof new Boolean(false)?

      1. @SrZorro 4y

        Its still an object

    2. @LionElJonson 4y

      Why typeof is not boolean?

      1. @RiedleroD 4y

        because it's a Boolean(), not a boolean

        1. @azizhakberdiev 4y

          Moreover, we are creating new instance of Boolean using new. It's no longer a primitive type, it is object

          1. @RiedleroD 4y

            yes, that's what I meant

      2. @SrZorro 4y

        typeof new Boolean() === "object" because the Boolean object is an object wrapper for a boolean value https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

        1. @LionElJonson 4y

          Thanks

  16. @VladislavSmolyanoy 4y

    Could it ever be false though?

  17. @SrZorro 4y

    .value is undefined, if you want the typeof boolean value you have to use .valueOf

  18. @saidov 4y

    Another noobie frontender JS meme making fun of JS

  19. @azizhakberdiev 4y

    let num = 123 num.toString() // "123" 123.toString() // err 123..toString() //"123"

    1. @ZgGPuo8dZef58K6hxxGVj3Z2 4y

      I guess its because of commas? Like 5.5.toString() works too?

      1. @azizhakberdiev 4y

        Interpreter thinks that after first dot comes floating part. When it comes to the second one it will recognize it as object's dot

        1. @ZgGPuo8dZef58K6hxxGVj3Z2 4y

          Yeah that's what I was thinking.

  20. @Agent1378 4y

    LOL. Reminds of al of those nasty UDs in C....

  21. @ZgGPuo8dZef58K6hxxGVj3Z2 4y

    😂😂😂😂😂

  22. @cringy_frog 4y

    100% legit, not only in JS

  23. Deleted Account 4y

    Lol Js ..

  24. @ZgGPuo8dZef58K6hxxGVj3Z2 4y

    Is it powered by JON?

  25. dev_meme 4y

    Lol its because any object is true. Also the automatic casting converst values to something they make sense. At least they try.

  26. @almaredonfaust 4y

    Fuck this meme. I don't know to much about JS, but got experience in another languages and in most cases "new" will return you either true or false depending on if object was created or not.

    1. dev_meme 4y

      you're talking nonsense in any high-level language

  27. @lol123a10 4y

    Just use Boolean instead of new Boolean. Everything just works perfectly. :)

  28. @ciser0 4y

    actually it's a joke from statically typed language perspective but the moment you understand dynamically typed languages it stops to be funny as you can see new Boolean is an object wrapper for a boolean value. there are four state of a variable in Js: null, undefined, true(here is where dynamics apply if variable exists it is true) or false i will not go into details. so when you pass an argument to the first object wrapper it accepts the above states of the variable if the value of the argument is not of type boolean

Use J and K for navigation