JavaScript – Fixing If Statement with Boolean and String Comparison Issues

javascript

In my code I have the following:

console.log(props.acknowledged) // false
console.log(props.type === "SOMETHING") //false
console.log(!props.acknowledged && !props.type === "SOMETHING") //false

How is that a !false && !false results to a false?

Best Answer

You need to add parantheses like this: !(props.type === "SOMETHING")

The priority of ! is more than === operator so your code will be evaluated as below:

!(props.type) === "SOMETHING"

so if props.type equals say "something else", it will become binary value of false and your whole expression is something && false which is false.

I think what you wanted to to is:

!(props.type === "SOMETHING")

or even

props.type !== "SOMETHING"
Related Question