Every value has an associated boolean, true or false, value in JS. For example, a null value has an associated boolean value of false. A string value, such as abc has an associated boolean value of true.
Values that are associated with boolean true are said to be truthy. Values that are associated with boolean false values are said to be falsy.
A full list of truthy values can be found here:
A full list of falsy values can be found here:
A single “!” symbol in javascript, also called a “bang”, is the logical “not” operator. If you place this operator in front of a boolean value, it will reverse the value, returning the opposite.
If a bang (“!”) returns the opposite truthy value, what would a bang executed twice on a value do?
So, running a bang twice determines the opposite of value, and then returns the opposite of that.
With non-boolean values, it is kind of cool to see what the double bang does:
Knowing the associated boolean value of any given value is helpful if you want to quickly reduce a value to a boolean:
In this case, if an account.cash value is greater than zero, the account.hasMoney will be true.
Let’s see another example:
The post Javascript convert to boolean using !!(double bang)operator appeared first on Andela.