Skip to content
Advertisement

The negation operator in JS

Why in this case

if (! x === y) {
console.log ('true');
} else {
console.log ('false');
}

gets false since the negation operator is used. Without the negation operator it also gets false because the operator ===checks if it compares the same values with the same data type. Why, then, after putting the exclamation point directly after x, does he still get false, since it contradicts falsehood, so I should get the truth?

Advertisement

Answer

!x negates x, then compares that to y. Assuming x is truthy, !x is false. So unless y is exactly false, !x === y is false. Read as: negated x equals y.

x === y is probably also false because the two values aren’t equal.

x !== y is the negated comparison operator, meaning “is not equal”. So where x === y is false, x !== y is true.

x !== y is equivalent to !(x === y), negating the entire result, not just x.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement