JavaScript: converting values to boolean or what is false and what is true

JavaScript: converting values to boolean or what is false and what is true

What will !x return? And !!x? It depends on what is x

!x will convert any value to a boolean, and !!x will negate the previous value. When x is a boolean, !x is its negation and !!x is back the original value x. But for other types, some magic happens thanks to auto conversion.

What exactly? I’ll explain in several cases. JavaScript recognizes 7 primitive data types, of which the most prominent ones are:

Primitive data types

Boolean type. It stores either true or false and it’s easy to guess what will the negation !x do.

Number type. It stores an integer or floating point number. 0 is converted to false and anything else is converted to true.

My related project

Timeline Creator

Create and export timeline graphs using an online tool

String type. It stores a sequence of characters. An empty string is converted to false and anything else (including " ") is converted to true.

Special types. There are two special types: null and undefined. Both are treated as false.

Summary

You can find the summary in the following reference table:

x (value)
!x
!!x
SPECIAL
undefined
TRUE
FALSE
null
TRUE
FALSE
NUMBER
0
TRUE
FALSE
1
FALSE
TRUE
STRING
“”
TRUE
FALSE
“a”
FALSE
TRUE
BOOLEAN
false
TRUE
FALSE
true
FALSE
TRUE
0 0 votes
Article Rating

Want more?

Share!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x