Double not (!!) is used to convert any value to its corresponding boolean primitive. That is, a truthy value to primitive true and a falsy value to primitive false. The conversion is based on the truthy and falsy value of the operand used with double not (!!).
How it works?
- In first step, first not (
!) returnsfalseif its operand can be converted totrue; otherwise, returnstrue. So in the first step, atruthy value is converted tofalseand afalsyvalue is converted totrue. - The second not then again does the same; returns
falseif its operand can be converted totrue; otherwise, returnstrue.
Examples
- Number 5 is a truthy value, so
!!5istrue.
console.log(!!5); // 5
2. empty string is falsy, so !!'' is false.
console.log(!!''); //false
3. Any object is truthy, so !!{} is true.
console.log(!!{});
The same can be achieved using Boolean function.
console.log(Boolean(5)); //true
console.log(Boolean("")); //false
console.log(Boolean({})); // true
