This punctation emoji of a double exclamation mark features two big red exclamation points that can be used to express surprise, shock, or to really emphasize or drive home a point.
Many people have actually no idea what they should and should not say over such a medium. That's why we have the double exclamation point. This is like saying, “I DON'T KNOW WHAT I FEEL, BUT I SURE DO FEEL SOMETHING!” or “THAT IS CRAZY!” or “AHHHHHHH!!!!!!” but not “AHHHHHHH!!!!!!” in a good way.
Multiple Exclamation Mark Madness!In formal writing, you include one exclamation mark at the end of a sentence. But in informal writing, such as emails or text messages, people often use more than one to emphasize their point. For example: "No!!!
A double exclamation point icon indicates a high-priority message. Regards.
This converts a value to a boolean and ensures a boolean type.
"foo" // Evaluates to "foo".
!"foo" // Evaluates to false.
!!"foo" // Evaluates to true.
If foo.bar
is passed through, then it may not be 0 but some other falsy value. See the following truth table:
Truth Table for javascript
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
" \t\r\n" == 0 // true
Source: Doug Crockford
Javascript also gets really weird when it comes to NaN values. And this is the only case I can think of off the top of my head where !! would behave differently to ===.
NaN === NaN //false
!!NaN === !!NaN //true
// !!NaN is false
I think the answer is that there isn't really much point. We can speculate about how it came about:
someVar
in multiple places, or in ways that genuinely benefited from having true
or false
, so this made more sense.!!
to convert to true
/false
that (s)he didn't even notice that it wasn't necessary here.new Boolean(false)
is a true-valued value), the person who wrote the function feels that it should always be done explicitly rather than implicitly — even though the effect is the same — just to call attention to it as a potential point of error.
!!
as an "explicit" Boolean conversion. Technically it's not — it uses the same implicit Boolean conversion that if
does — but if you're used to this idiom, then it amounts to an explicit conversion.but in my subjective opinion, none of those reasons is a very good one!
As stated above, it forces an object with a boolean type. You can see for yourself:
(function typecheck() {
var a = "a";
var b = !a;
var c = !!a;
console.log("var a =", a, typeof(a))
console.log("var b =", b, typeof(b))
console.log("var c =", c, typeof(c))
})();
If you are simply doing comparisons, the conversion merely saves you a type coercion later on.
FYI, the following values are coerced to FALSE in JavaScript:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With