JavaScript : Double NOT operator

Double NOT operator

The double NOT operator can be used with an expression. It returns the boolean state of the expression. The double NOT operator is internally used by the Boolean function to convert to a boolean value.

let v = !!10;
console.log(v, v.constructor.name); // true Boolean

let v2= !!"Javascript";
console.log(v2,v2.constructor.name); // true Boolean

let v3 = !!"";
console.log(v3, v3.constructor.name); // false Boolean

let v4 = !!null;
console.log(v4, v4.constructor.name); // false Boolean

In the above code, the double NOT operator is used to get the boolean state of the expression.

Boolean Object

Do not use Boolean object to perform conversion to Boolean type, instead use the Boolean function. The Boolean object is an object and hence will always be truthy in conditional evaluations.

// do not use - new Boolean to convert - non boolean to boolean
let v = new Boolean(10);
console.log(v, v.constructor.name); // Boolean(true)   Boolean
console.log( true == v, true === v); // true false 

let v2 = new Boolean("true");
console.log(v2);//Boolean(true)

if (v2)
  console.log("v2-true"); // true

let v3 = Boolean(false);

console.log(v3, v3.constructor.name); // true  Boolean
console.log( true == v3, true === v3); // true true