JS - 2 : How to Convert any value to boolean?

Mahabubur Rahman
0

Convert any value to Boolean.

There are only two Boolean value true and false. But some time we need to convert string to Boolean. So today I will show how to convert any value to Boolean.

Let see when we console log any string value it will show as bellow -

console.log("Human") // Human
console.log(!!"Human") // true

So we understand that Using !! in front of any value we can convert the value to Boolean

It is same as Boolean() function. See the following examples - 

console.log(!!"Human") // true
console.log(!!1) // true
console.log(!!0) // false
console.log(!!"") // false
console.log(!!" ") // true
console.log(!!undefined) // false
console.log(!!NaN) // false
console.log(!!null) // false
console.log(!!true) // true

So it is the same and short form of Boolean().

Post a Comment

0Comments
Post a Comment (0)