Changing a boolean variable using '!'

Hello everyone, I’m new.

I searched but couldn’t come up with the answer on these forums.

In the flow control review, it says:

! changes a variable that is true to false, and vice versa.

But the course never covered that or gave an example of the syntax.

Anyone have an example? Thanks.

When given a boolean such as true or false, the NOT operator, ! negates them.

! true   =>  false
! false  =>  true

Think in terms of, “If it is not true, then it is false, else if it is not false, then it is true.”

The operator can also be used to convert any expression to a boolean.

! 0   =>  true
! 1   =>  false
! ""  =>  true
! "A" =>  false

In other words, anything NOT truthy is false, and NOT falsy is true.

We need to understand what truthy and falsy values are. Any that are,

'' or "", 0, null or undefined are falsy.

All others are truthy.


I may have to read over that a few more times, but thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.