Create a boolean flag
Normalize an optional or mixed-type expression before storing or passing a flag.
JS Puzzles · Const
Double negation (!!) converts any JavaScript value to a boolean primitive using its truthiness. It is equivalent to Boolean(value).
The first ! converts a value to a boolean and reverses it; the second ! reverses that boolean again. The result is therefore the ordinary truthiness conversion, equivalent to Boolean(value), producing only true or false.
!!valueA boolean primitive: true or false.
const values = ["hello", "", 0, 42, null, {}];
const flags = values.map(value => !!value);
console.log(flags);
// [true, false, false, true, false, true]Applying ! twice converts each value to its boolean equivalent.
Normalize an optional or mixed-type expression before storing or passing a flag.
Convert a value while composing a larger logical expression that expects a boolean operand.
Make a value's truthiness explicit in a compact expression.
Double negation uses two logical NOT operations: the first converts the operand to a boolean and inverts it, while the second inverts that result. The final value is therefore true or false according to the operand's truthiness.
Follow-up: How can the same conversion be written without !!? Use Boolean(value), which performs the direct boolean conversion.
Falsy values such as 0, an empty string, null, undefined, and NaN become false; other values become true. Thus !!value reflects JavaScript's boolean-conversion rules rather than preserving the original type.
Follow-up: What about the string "0"? It is non-empty, so it converts to true.
The expression is written as !!value. Since logical NOT has high precedence, a chain such as !!value && other is evaluated as (!!value) && other.
Follow-up: How can precedence be made explicit? Add parentheses, for example (!!value) && other.
Logical NOT (!)Boolean conversionLogical AND (&&)Logical OR (||)