Search⌘ K
AI Features

Discussion: Truth or Fiction?

Learn to use the double negation !! operator to convert values to boolean in JavaScript. Understand truthy and falsy values and apply this knowledge to filter arrays efficiently with arrow functions, improving your coding clarity and precision.

Verifying the output

Now, it’s time to execute the code and observe the output.

Javascript (babel-node-es2024)
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. ...