Write if (!condition) to run a block when condition is falsy. The ! operator converts its operand to a boolean and reverses the result. A negated if in JavaScript is useful for a simple guard, provided that every falsy value should lead to the same branch.
A negative condition in JavaScript does more than compare a value with false: it also treats 0, an empty string, null, and undefined as falsy. When those values mean different things in your application, a more specific comparison is clearer.
If Not JavaScript: Try a Guard
A guard handles an unwanted state before the rest of the function continues:
function welcome(isSignedIn) {
if (!isSignedIn) {
return 'Please sign in.';
}
return 'Welcome back.';
}
console.log(welcome(false)); // Please sign in.
console.log(welcome(true)); // Welcome back.
Here isSignedIn is a boolean, so the negation reads naturally as “if not signed in.” If the user is not signed in, the function returns early; otherwise it reaches the welcome message. A guard can keep the main path less deeply nested, but the condition should describe the state being checked.
In JavaScript, if not all conditions are already booleans, the operator still converts its operand to a boolean before reversing it. Test the values the function can actually receive rather than assuming that !value means only value === false. The example expects a boolean; if not, JavaScript applies its truthiness rules to the argument. The MDN logical NOT reference details the conversion.
See which values are falsy
The common falsy values are false, 0, -0, 0n, '', null, undefined, and NaN. Browsers also have a legacy exception, document.all. Most other values are truthy, including the strings '0' and 'false', empty arrays, and ordinary empty objects.
console.log(!0); // true
console.log(!''); // true
console.log(!null); // true
console.log(!'false'); // false
console.log(![]); // false
console.log(!{}); // false
For a negated JavaScript condition, this means an empty array does not take the branch merely because it has no elements. Check items.length === 0 when emptiness is what matters. Likewise, check an object’s relevant properties instead of assuming that an object with no keys is falsy.
A conditional negation in JavaScript returns a boolean, not the original value. The variable itself is unchanged: !value creates a result for the condition without assigning anything back. If the same value is needed later, it retains its original type and contents. You can inspect Boolean(value) to see how a value is classified, then compare that result with the branch you intend. This is especially useful for data read from a form, where even a field containing the characters 0 normally supplies a string.
Do not confuse zero with absence
Consider a retry setting. Zero can be a deliberate instruction not to retry; a missing setting may instead require a default:
function retries(value) {
if (value === null || value === undefined) {
return 3;
}
return value;
}
console.log(retries(0)); // 0
console.log(retries(undefined)); // 3
Using if (!value) here would replace zero as well as a missing value. If the goal is simply a nullish default, value ?? 3 preserves 0, false, and '' while replacing null and undefined. It does not validate type or range; a separate check may still be needed for a retry count. A numeric setting should be a nonnegative integer if that is the contract; if not, JavaScript will still accept values such as a string unless the code checks them.
In JavaScript, if not all empty strings are invalid, do not use truthiness as a substitute for a requirement. An empty label may be allowed, while a missing label may need attention. Write down the expected behavior for '', 0, false, null, and undefined before choosing a test. A JavaScript condition using NOT is only as accurate as that decision.
Compare negation and inequality
!value asks whether a value is falsy. value !== expected asks whether two values are strictly unequal. Those are different questions:
const value = 0;
console.log(!value); // true
console.log(value !== false); // true
console.log(value !== 0); // false
In JavaScript, if not equal to a particular value is the requirement, !== often expresses it directly. Strict inequality treats values of different types as unequal, so 0 !== false is true. Loose inequality (!=) can perform conversions; use it only when those rules are intended and understood. See MDN’s strict inequality reference.
Negating an entire comparison is also possible: !(status === 'ready') has the same boolean result as status !== 'ready'. Parentheses make the intended grouping explicit. In more complex expressions, pay attention to operator precedence and use parentheses to make the condition readable; a misplaced ! can negate one operand instead of the comparison.
Use double negation for a boolean
!!value applies logical NOT twice. The result has the same truthiness as the original value but is the actual boolean true or false:
console.log(!!'hello'); // true
console.log(!!0); // false
console.log(!![]); // true
Boolean(value) performs the same conversion and can be easier to read. Inside an if condition, the conversion already happens, so if (!!value) usually adds no useful behavior. An explicit conversion can help when returning a boolean from a function or storing one for an API that expects a boolean.
Choose the condition that matches the question
Use a JavaScript if not guard for a boolean state or when all falsy values really do mean the same thing. Use !== for a comparison with a specific value, an explicit nullish check for absence, and a length check for an empty collection. Use !! or Boolean() when a boolean result is required outside a condition.
Test each negated JavaScript condition with both ordinary and boundary inputs, and compare the actual branch with the expected result. If zero, an empty string, or false is valid, include it in the test cases. A short condition is useful only when it preserves the meaning of the data passing through it. When reviewing an inverted condition in JavaScript, read both branches aloud and verify the names match the behavior. A positive name such as isReady is usually easier to negate than a double negative such as isNotUnavailable.
