Discussion: Truth or Fiction?
Execute the code to understand the output and gain insights into boolean conversion and truthy vs. falsy values.
We'll cover the following...
Verifying the output
Now, it’s time to execute the code and observe the output.
Press + to interact
const arr = [1, 8, NaN, 15, ""];const newArr = arr.filter(function(item) {return !!item});console.log(newArr);
NOT NOT
The code !!
in JavaScript can be used as a quick way of converting a value into either true
or false
in a sneaky way. First, the !
symbol is the logical NOT operator. It negates the value that follows it. For example, !true
would result in false
, and !false
would result in true
.
Now, if we apply the !
operator twice, it’s like saying “not not.” It might sound weird, but it’s actually a clever trick. The first !
flips the value, and the second !
flips it back to what it originally was. ...