DEV Community

Cover image for The double not operator (!!)
Accreditly
Accreditly

Posted on • Updated on • Originally published at accreditly.io

The double not operator (!!)

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.

Top comments (5)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

Your JS example works equally well with !! removed. There's no point it being there. I've not touched PHP in years, but I think the same applies to that example too.

This is the whole point of truthy and falsy values. Granted, there are some times where you might actually want an actual Boolean value - but your examples do not show this.

Collapse
 
pengeszikra profile image
Peter Vivo • Edited

I using double not many times in react like this.

{!!list.length && list.map(({text,key}) => <p key={key}>{text}</p>)}
Enter fullscreen mode Exit fullscreen mode

In my read this is always part of js and do not count magic.

Js magic start here:

[[[42]]]+[] // = '42'
// or
+!!{} // = 1
// or
(+!!{}+!!{}+!!{}<<+!!{}+!!{}+!!{}+!!{})-!!{}-!!{}-!!{}-!!{}-!!{}-!!{} // = 42
Enter fullscreen mode Exit fullscreen mode
Collapse
 
moopet profile image
Ben Sinclair

I can't think of any good reason to use it. If you need a boolean value rather than something that's truthy or falsey then it's a few more keystrokes to use something explicit like boolval or Boolean for the same effect, but much more clarity.

Collapse
 
nicolus profile image
Nicolus

I won't talk for javascript because I'm really not an expert, but I'd never accept a PR in PHP with
!!$isAdmin. It's just begging for a catastrophic typo and there's no reason to use a hack like this when there's a language construct that's meant to cover this exact usecase : (bool)$isAdmin.

Plus if at this point you don't know the type of $isAdmin it's probably a sign that some of ypur method are not typed strictly enough.

Collapse
 
accreditly profile image
Accreditly • Edited

I don't disagree. As a rule, I don't use it, but it's not a bad thing to understand it. I've seen it out in the wild many times on some large and well maintained projects, across many languages (PHP included)