
The equality operators provide the best way to check for null. How to Check for Null in JavaScript with Equality Operators Let’s now see the two major ways you can check for null and how it relates to undefined. Undefined works well because when you check the type using the typeof() method, it will return undefined: let lastName Ĭonsole.log(typeof(lastName)) // undefined In contrast, a variable has the type of undefined when you declare it without initiating a value. Null and undefined are very similar in JavaScript and are both primitive types.Ī variable has the type of null if it intentionally contains the value of null. So how can you now check for null? This article will teach you how to check for null, along with the difference between the JavaScript type null and undefined. But unfortunately, this returns “object” because of an historical bug that cannot be fixed. This means you are supposed to be able to check if a variable is null with the typeof() method.
