I’ve been wondering why not window.chrome == true or Boolean(window.chrome), but it turns out that the former doesn’t work and that == has essentially no use unless you remember some completely arbitrary rules, and that JS developers would complain that the latter is too long given the fact that I’ve seen javascript code using !0 for true and !1 for false, instead of just true and false because they can save 2 to 3 characters that way.
== has essentially no use unless you remember some completely arbitrary rules
If you make sure the types match, like by explicitly converting things on the same line on that example, then you can use it just like if it was ===.
In fact, there are people that defend that if your code behaves differently when you switch those two operators, your code is wrong. (Personally, I defend that JS it a pile of dogshit, and you should avoid going to dig there.)
lol i did something like what i assume your goal is on my neocities when i detect
!!window.chrome === true
Why the double negation?
It’s a handy way to convert any value to a Boolean. If
window.chrome
is defined and done non-empty value, double negation turns it into justtrue
.I’ve been wondering why not
window.chrome == true
orBoolean(window.chrome)
, but it turns out that the former doesn’t work and that==
has essentially no use unless you remember some completely arbitrary rules, and that JS developers would complain that the latter is too long given the fact that I’ve seen javascript code using!0
for true and!1
for false, instead of justtrue
andfalse
because they can save 2 to 3 characters that way.I’ve never seen the
!0
and!1
, it is dumb and indicates either young or terrible devs.Boolean(window.chrome)
is the best,!!window.chrome
is good, no need to test if it’s equal totrue
if you make it a boolean beforehand.If you make sure the types match, like by explicitly converting things on the same line on that example, then you can use it just like if it was
===
.In fact, there are people that defend that if your code behaves differently when you switch those two operators, your code is wrong. (Personally, I defend that JS it a pile of dogshit, and you should avoid going to dig there.)
JS “idiomatic” way to cast to boolean. But could just be written as
!window.chrome
instead.