3
sjwsjwsjw
11h

I don't understand javascript

Need help understanding the using !!fooBar vs fooBar when checking ?(truthiness?) I'm not sure what is being checked.

I'm used to seeing the latter fooBar.

Comments
  • 3
    Both evaluate truthyness, but the first one explicitely "casts" it into a bool

    E.g.

    let notEmpty = someString; // notEmpty is a string

    let notEmpty = !!someString; // notEmpty is a bool

    If used in an if condition or with other boolean operators it doesn't matter since they automatically coerce it into a bool
  • 1
    JS haxx0rs love obfuscation so they'd rather use this than typing fooBar.isEmpty or something similarly readable.
  • 1
    It probably comes from a C programmer.

    It was used when you wanted to cast a "bool" (C does not have bool) into either a 0 or a 1.

    Technically true is any nonzero integer, but you could do some nifty math tricks by doing the !! And thus ensuring true would be 1 and not just any nonzero.
  • 1
    !!booBar is Boolean(fooBar)
  • 0
    @kiki Oh, darling, if !!booBar is your take on Boolean(fooBar), then your coding logic's as revolutionary as rediscovering the semicolon.
  • 0
    @princess semicolons are for dinosaurs
  • 0
    This reminds me of the +'a' in banana
  • 0
    @Lensflare fun fact, my dad have a semi colon. We had to remove half of it when he had a rectocolisis at 24. Dark shit, he literally puked poo at some point.
  • 0
    most JS complaints come from specific nitpicking. You don't need to use double ! if you hate it. I personally use strictly boolean variable for this kind of purpose, even though JS itself allows us to reassign it any other kind of data.
Add Comment