The JavaScript Logical Not ! operator
The logical NOT operator ! inverts a value to its opposite boolean — !true is false, and !0 is true. It first coerces the value to a boolean, then flips it. The double form !!value is a common idiom that converts any value to a real true/false.
Overview
! flips a value's truthiness. Put it before any value and you get the opposite boolean: !true is false, !"" is true (empty string is falsy), !user is true when user is missing. It evaluates the value's truthiness first, then negates it, so it always returns a real boolean.
The everyday use is negating a condition: if (!isValid) reads as "if not valid," and if (!arr.length) means "if the array is empty." It pairs with && and || to build up conditions, and binds tighter than both.
Doubling it — !!value — is a well-known idiom for converting to a boolean. The first ! negates (and coerces), the second negates back, leaving a clean true or false that matches the value's truthiness. !!"hello" is true, !!0 is false. It does the same thing as Boolean(value), just more tersely.
Syntax
!value
!true // false
!0 // true (0 is falsy)
!"" // true (empty string is falsy)
!!"hello" // true (convert to boolean)
if (!isReady) { ... } // negate a condition
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const values = [0, '', 'hi', [], null];
const lines = values.map(v =>
JSON.stringify(v) + ' -> !!v = ' + (!!v)
);
document.getElementById('out').textContent = lines.join('\n');
</script>
Best practices
- Use
!to negate a condition:if (!valid),if (!arr.length). - Use
!!value(or Boolean(value)) to coerce any value to a real boolean. - Remember it coerces first — it works on any value, not just booleans.
- It binds tighter than && and ||, so
!a && bis(!a) && b.
Frequently asked questions
What does the ! operator do?
!true is false.What does !! do in JavaScript?
! negates, the second negates back, giving true for truthy values and false for falsy ones — the same as Boolean(value).How do I check if an array is empty?
if (!arr.length) — an empty array has length 0, which is falsy, so !0 is true.Is !!value the same as Boolean(value)?
!!value is just a shorter, common idiom.