0

Code inspection (IntelliJ and GitHub) says that the following code can be simplified:

type Something = string | number | boolean | object
function isSomething(a: unknown): a is Something {
    return a != undefined && a != null;
}

because a != null is always true. Why is that? AFAIK undefined and null are different values.

1
  • 2
    null == undefined is true. If you really need to distinguish undefined and null, use the === and !== operators. Commented Oct 28, 2022 at 13:16

1 Answer 1

3

a != undefined is false if a is null. That means if a is null, the second condition will never run, since the && operator will short-circuit.

console.log(null == undefined); // true
console.log(null != undefined); // false

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.