The double not operator (!!)

Accreditly - Jun 21 '23 - - Dev Community

In the realm of programming, operators are tools that allow us to perform operations on variables and values. One such operator that often catches developers off-guard is the double not operator (!!). This article delves into what the double not operator is, when and why to use it, and when to avoid it. We'll also explore examples in both PHP and JavaScript, two languages where this operator can be handy.

What is the Double Not Operator?

In both PHP and JavaScript (and many other languages), the ! symbol is a logical NOT operator. It negates the truthiness of the operand that follows it. If the value is truthy (i.e., evaluates to true in a boolean context), ! turns it to false and vice versa.

We've covered what it is in PHP extensively in our article The double not (!!) operator.

The double not operator (!!) is simply the ! operator used twice. The first ! converts the operand to its boolean opposite, and the second ! flips it back. The result is a value that is explicitly either true or false.

Let's look at an example in JavaScript:

let truthyValue = 'Hello, world!';
console.log(!!truthyValue); // Outputs: true

let falsyValue = '';
console.log(!!falsyValue); // Outputs: false
Enter fullscreen mode Exit fullscreen mode

And the same in PHP:

$truthyValue = 'Hello, world!';
var_dump(!!$truthyValue); // Outputs: bool(true)

$falsyValue = '';
var_dump(!!$falsyValue); // Outputs: bool(false)
Enter fullscreen mode Exit fullscreen mode

Why and When to Use the Double Not Operator

The double not operator is a quick and concise way to convert any value to its boolean equivalent. It's particularly useful in conditions where you need to ensure a variable's truthy or falsy nature and not its actual value.

In JavaScript, it can be beneficial due to the language's loose typing and because various values can be considered "falsy" (e.g., null, undefined, 0, NaN, empty string, etc.). Using !! can help normalize these values to a proper boolean.

Here's an example where !! can be used effectively in JavaScript:

let user = {
    name: 'John Doe',
    // isAdmin property is missing
};

if (!!user.isAdmin) {
    console.log('User is an admin');
} else {
    console.log('User is not an admin');
}
Enter fullscreen mode Exit fullscreen mode

In this case, because isAdmin is missing, !!user.isAdmin will return false, and 'User is not an admin' will be printed.

In PHP, the !! operator can also be useful in conditional checks, especially when dealing with values that could be considered "falsy" like 0, null, false, empty string, etc.

$isAdmin = 0;

if (!!$isAdmin) {
    echo 'User is an admin';
} else {
    echo 'User is not an admin';
}
Enter fullscreen mode Exit fullscreen mode

When to Avoid the Double Not Operator

While !! can be handy, it can also make code more difficult to read, especially for those unfamiliar with this operator. It's not as explicit as using methods like Boolean() in JavaScript or (bool) casting in PHP, which achieve the same result.

Here are equivalent examples without !!:

JavaScript:

let truthyValue = 'Hello, world!';
console.log(Boolean(truthyValue)); // Outputs: true
Enter fullscreen mode Exit fullscreen mode

PHP:

$truthyValue = 'Hello, world!';
var_dump((bool)$truthyValue); // Outputs: bool(true)
Enter fullscreen mode Exit fullscreen mode

If readability and clarity are a priority, or if you're working with developers who may not be familiar with !!, it may be best to use these more explicit methods to convert values to their boolean equivalent.

The double not operator (!!) is a quick, if somewhat cryptic, way to convert any value to a boolean in languages like JavaScript and PHP. While it's a clever trick and can come in handy, remember that it may come at the cost of readability. Use your judgment and consider your team's familiarity with such nuances when deciding whether to use it in your code.

If you're an experienced developer and have come across the double not operator before then you may be interested in both our PHP Fundamentals Certification and JavaScript Fundamentals Certification.

. . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player