How to check if a variable is not null?

They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null. The only values that are not truthy in JavaScript are the following (a.k.a. falsy values): null undefined 0 “” (the empty string) false NaN

JavaScript null check

An “undefined variable” is different from the value undefined. An undefined variable: A variable with the value undefined: When a function takes an argument, that argument is always declared even if its value is undefined, and so there won’t be any error. You are right about != null followed by !== undefined being useless, though.

JavaScript null check

An “undefined variable” is different from the value undefined. An undefined variable: A variable with the value undefined: When a function takes an argument, that argument is always declared even if its value is undefined, and so there won’t be any error. You are right about != null followed by !== undefined being useless, though.