Can Someone Explain This 'Double Negative' Trick

Can someone explain this 'double negative' trick?

A logical NOT operator ! converts a value to a boolean that is the opposite of its logical value.

The second ! converts the previous boolean result back to the boolean representation of its original logical value.

From these docs for the Logical NOT operator:

Returns false if its single operand can be converted to true; otherwise, returns true.

So if getContext gives you a "falsey" value, the !! will make it return the boolean value false. Otherwise it will return true.

The "falsey" values are:

  • false
  • NaN
  • undefined
  • null
  • "" (empty string)
  • 0

JavaScript: what is the meaning of !!?

It forces the type to become a true boolean value rather than a "truthy" value.
Examples:

var a = (1 === true) // comes out as false because 1 and true are different types and not exactly the same

var b = ((!!1) === true) // comes out as true because the 1 is first converted to a boolean value by means of negation (becomes false) and then negated a second time (becomes true)

Double negation (!!) in JavaScript - what is the purpose?

It casts to boolean. The first ! negates it once, converting values like so:

  • undefined to true
  • null to true
  • +0 to true
  • -0 to true
  • '' to true
  • NaN to true
  • false to true
  • All other expressions to false

Then the other ! negates it again. A concise cast to boolean, exactly equivalent to ToBoolean simply because ! is defined as its negation. It’s unnecessary here, though, because it’s only used as the condition of the conditional operator, which will determine truthiness in the same way.

What is the !! operator in javascript?

Double negation !! in JavaScript simply converts values to boolean type.

Double Negation in C++

It's a trick to convert to bool.

difference between if (!!variable), and, if (variable)?

There is a difference for assigning it, but not for using it in a conditional statement. The reason the !! is used is because the first ! will convert your variable to its truthy evaluation and then not it. So "hello" becomes true, is then negated, becomes false, and the second ! will negate the false, resulting in true. This can be desirable when trying to obtain the thruthy value from a variable. However, there is not much gained by doing it in an if statement.

What is the purpose of a double negative in macro definition, like (!!(expr))?

It is a way to cast an expression to bool. In C++ though, operator! can be overloaded. Another way for C/C++:

0 != (expr)

C++ only way:

static_cast<bool>(expr)

[Edit]
Thinking more about C++ operator overloading, it make sense to avoid using operators. Libraries like Boost.Spirit and Boost.Lambda use expression templates and lazy evaluation, so that expressions like (expr) || call() may behave not as expected. The most bullet proof version of that macro looks like this:

#define uassert(expr) if(expr) {} else { uasserted(...); }

Here, only conversion of expr to bool is used. The else branch is needed to protect from expressions like uassert(x) else something_else();.

Why the linux kernel uses double logical negations instead of casts to bools?

There was no bool type when Linux was first written. The C language treated everything that was not zero as true in Boolean expressions. So 7, -2 and 0xFF are all "true". No bool type to cast to. The double negation trick ensures the result is either zero or whatever bit pattern the compiler writers chose to represent true in Boolean expressions. When you're debugging code and looking at memory and register values, it's easier to recognize true values when they all have the same bit patterns.

Addendum: According the C89 draft standard, section 3.3.3.3:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int . The expression !E is equivalent to (0==E).

So while there was no Boolean type in the early days of the Linux OS, the double negation would have yielded either a 0 or a 1 (thanks to Gox for pointing this out), depending on the truthiness of the expression. In other words any bit pattern in the range of INT_MIN..-1 and 1..INT_MAX would have yielded a 1 and the zero bit pattern is self-explanatory.

What is the !! (not not) operator in JavaScript?

It converts Object to boolean. If it was falsey (e.g., 0, null, undefined, etc.), it would be false, otherwise, true.

!object  // Inverted Boolean
!!object // Noninverted Boolean, so true Boolean representation

So !! is not an operator; it's just the ! operator twice.

It may be simpler to do:

Boolean(object) // Boolean

Real World Example "Test IE version":

const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // Returns true or false

If you ⇒

console.log(navigator.userAgent.match(/MSIE 8.0/));
// Returns either an Array or null

But if you ⇒

console.log(!!navigator.userAgent.match(/MSIE 8.0/));
// Returns either true or false

What is meaning of !! in C -- and why is it needed?

It's a double-negative. It's a way of converting an otherwise-non-bool expression (such as flags & GST_SEEK_FLAG_FLUSH) to a bool. I personally prefer:

flush = (flags & GST_SEEK_FLAG_FLUSH) != 0;



Related Topics



Leave a reply



Submit