4 ms·
Yeah, a lot of these have nothing to do with JS. I have no idea what the site is trying to accomplish. I mean: !!!true In what language does that (or its
by fenomas 5y ago
Yeah, a lot of these have nothing to do with JS. I have no idea what the site is trying to accomplish.
I mean:
!!!true
In what language does that (or its equivalent) not evaluate to false?
- dolmen 5y agoIn Go: https://play.golang.org/p/G8jOKtIj4AY https://play.golang.org/p/G8jOKtIj4AY
- Sebb767 5y agoCan anyone explain this to me?
- svieira 5y agoLooks like the `true` and `false` _bindings_ in Go are mutable and can be re-assigned. The same thing was possible in Python 2, IIRC: False, True = True, False # Have fun debugging!
- Sebb767 5y agoAh, I missed that line. Now I feel stupid ;)
- Cthulhu_ 5y agoThey're defining a const named true and false to their inverse values, shadowing the builtin true/false keywords.
- carl_dr 5y ago!!!true is equal to false in JS, and in Go (and in any other language I can think of.) In that Go example above, the author is reassigning true and false to be their opposites.
- JoeCortopassi 5y ago`!` is added to values in most languages as a shorthand for saying "give me the opposite boolean value of this. So `!true` would equal `false` Some people add two exclamation points as a shorthand to cast a value to a boolean. So if you wanted to see if something was 'truthy', you could say `!!truthyValue` and it would return ` true` instead of the value itself. Literally what your asking the language is "give me the opposite boolean value of the opposite boolean value of `truthyValue`" Now you can probably see why three exclamation points is silly, it's not giving you anything that a single exclamation point wouldn't give you. Both `!truthyValue` and `!!!truthyValue` evaluate to false, you are literally saying "give me the opposite boolean value of the opposite boolean value of the opposite boolean value of `truthyValue`" The example in Go is intentionally misleading because Go lets you reassign the values for `true` and `false`. It's going through all the same steps I described above, but it's starting with a value opposite of what you think it is
- 7steps2much 5y agoOne might want to add that it does work when you use a variable however. https://play.golang.org/p/6UfNFWm_-JR https://play.golang.org/p/6UfNFWm_-JR
- jakelazaroff 5y agoThe issue isn't that they're constants, but that GP called them "true" and "false" and assigned the opposite values you'd expect. You can break it in exactly the same way with variables: https://play.golang.org/p/EVU84l0A57I https://play.golang.org/p/EVU84l0A57I Kinda crazy to me that Go doesn't reserve the words "true" and "false", but ¯\_(ツ)_/¯
- deleted 5y ago[deleted]
- fenomas 5y agoGreat point, I'll edit my comment to say "(or its equivalent)", instead of "(that precise sequence of characters, no matter what you've redefined true and false to be)". Not sure why I originally wrote it that way.