It has been pointed out to me that this posting is bad. The double exclamation points are not an operator in and of themselves. Instead, they are two ! operators. This scenario...
return !foo ? 42 : 13;
...is going to return 42 if foo is zero/false/undefined (see this for more) and 13 otherwise. When we add an extra exclamation mark like so...
return !!foo ? 42 : 13;
...we are just doing the opposite. The !! is akin to the null coalescing operator of C# in effect (in this example in tandem with the question mark in that the question mark starts behaving like the double question marks of C#) and thus is a little easier for someone like me who knew C# before JavaScript to read. The first (right) exclamation does a conversion from what may be to a boolean type. The second (left) exclamation just flips that value. Thanks to grnndaddy for this explanation.
No comments:
Post a Comment