You can use the qualities of the abstract equality operator to do this:

if (variable == null){
    // your code here.
}

Because null == undefined is true, the above code will catch both null and undefined.

Answer from Sarfraz on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-for-null-in-javascript
JS Check for Null – Null Checking in JavaScript Explained
November 7, 2024 - This means you are supposed to be able to check if a variable is null with the typeof() method. But unfortunately, this returns “object” because of an historical bug that cannot be fixed. let userName = null; ...
Discussions

What's the best way to verify if a JavaScript variable is empty, null, or undefined?
Hey everyone, I’m trying to figure out a reliable method to check if a JavaScript variable is empty, null, or undefined. I’ve come up with a function, but I’m not sure if it covers all the bases. Here’s what I’ve got: f… More on community.latenode.com
🌐 community.latenode.com
0
0
April 18, 2025
Basic JS question: when to check for undefined, null, etc

TL;DR: Use value != null. It checks for both null and undefined in one step.

In my mind, there are different levels of checking whether something exists:

0) 'property' in object - Returns true if the property exists at all, even if it's undefined or null.

  1. object.property !== undefined - Returns true if the property exists and is not undefined. Null values still pass.

  2. object.property != null - Return true if the property exists and is not undefined or null. Empty strings and 0's still pass.

  3. !!object.property - Returns true if the property exists and is "truthy", so even 0 and empty strings are considered false.

From my experience, level 2 is usually the sweet spot. Oftentimes, things like empty strings or 0 will be valid values, so level 3 is too strict. On the other hand, levels 0 and 1 are usually too loose (you don't want nulls or undefineds in your program). Notice that level 1 uses strict equality (!==), while level 2 uses loose equality (!=).

More on reddit.com
🌐 r/javascript
15
17
December 20, 2017
Good way to check for variable being not null and not undefined.
There are some style guides that basically say you always should use ===, but you can use == in order to check for null or undefined at the same time. So you could do the following: if (value != null) { // This will run if `value` is not `null` and not `undefined`. } More on reddit.com
🌐 r/javascript
57
32
October 20, 2016
QuerySelector, Check If Element Exists

The problem here is that the copy function doesn't work on falsy values. It'll work if you switch in

else hqLink = " ";
More on reddit.com
🌐 r/learnjavascript
4
1
January 1, 2020
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
The null keyword refers to the null primitive value, which represents the intentional absence of any object value. function getVowels(str) { const m = str.match(/[aeiou]/gi); if (m === null) { return 0; } return m.length; } console.log(getVowels("sky")); // Expected output: 0
🌐
DEV Community
dev.to › wolfhoundjesse › null-checking-in-javascript-lc4
Null-checking in JavaScript - DEV Community
April 11, 2019 - ... is undefined, null, or "", or any other falsy valuue... then the if will short circuit and not perform any of the other checks. This means it is impossible for the tokenInfo !== undefined check (and others) to ever evaluate to true. ... The other checks are unnecessary as they won't change the function at all. ... I think the concepts of truthy and falsy values is both the best and worst thing about javascript.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
Finally, to check if a value has been declared and assigned a value that is neither null nor undefined, use typeof: typeof maybeUndeclared !== "undefined" && (typeof maybeUndeclared !== "object" || !maybeUndeclared) Now go out there and check for null with confidence. In JavaScript, null represents an intentional absence of a value, indicating that a variable has been declared with a null value on purpose.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - Checking undeclared variable: true ... null nor undefined: true false is neither null nor undefined: true · Lodash _.isNull() method is used to find whether the value of the object is null....
🌐
LogRocket
blog.logrocket.com › home › how to check for null, undefined, or empty values in javascript
How to check for null, undefined, or empty values in JavaScript - LogRocket Blog
February 14, 2025 - Learn how to write a null check function in JavaScript and explore the differences between the null and undefined attributes.
🌐
Programiz
programiz.com › javascript › examples › check-undefined-null
JavaScript Program To Check If A Variable Is undefined or null
To understand this example, you ... is undefined or null function checkVariable(variable) { if(variable == null) { console.log('The variable is undefined or null'); } else { console.log('The variable is neither undefined nor null'); } } let newVariable; checkVariable(5); ...
🌐
Coderanch
coderanch.com › t › 734748 › languages › check-variable-null-JavaScript
Is there a better way to check if variable is null value or not in JavaScript? (Server-Side JavaScript and NodeJS forum at Coderanch)
September 17, 2020 - There are three approaches you can take, depending on what a represents and what you want your application to do with it: 1) Check that a is exactly null: if (a === null) { 2) Check that a is similar to null: if (a == null) { 3) Check that a is falsey: if (!a) { The most common approach is ...
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_null_checking.htm
JavaScript - Null Checking
The Object.is() function in JavaScript that compares two values to see whether they are the same. A boolean value indicates if the two parameters in the function have the same value. Two values could be the same if both the values are null. ... <!DOCTYPE html> <html> <head> <title>Check for null values in JavaScript</title> </head> <body> <h2>Check for null values using the Object.is() method...
🌐
Latenode
community.latenode.com › other questions › javascript
What's the best way to verify if a JavaScript variable is empty, null, or undefined? - JavaScript - Latenode Official Community
April 18, 2025 - Hey everyone, I’m trying to figure out a reliable method to check if a JavaScript variable is empty, null, or undefined. I’ve come up with a function, but I’m not sure if it covers all the bases. Here’s what I’ve got: function checkIfEmpty(value) { return !value || value.length === 0; } Does this look good to you?
🌐
DEV Community
dev.to › anuva312 › 5-simple-ways-to-check-for-undefined-or-null-values-1bfb
5 Simple ways to check for undefined or null values - DEV Community
March 5, 2023 - Here, I have used the Strict equality (===) operator, but feel free to use the normal Equality operator like in the previous example for if...else. Both will give the same output. Now is there a better way to do this? With less code? Actually there is! ... In my opinion, using the nullish coalescing operator (??) is a wonderful way to check for nullish values during assignment.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › check-if-a-variable-is-undefined-or-null
JavaScript Program to Check If A Variable Is undefined or null | Vultr Docs
November 6, 2024 - The results are true if the comparisons match exactly, illustrating that a is undefined and b is null. Check if a variable is undefined by using the typeof operator.
🌐
Scaler
scaler.com › home › topics › javascript program to check for null
JavaScript Program to Check for Null - Scaler Topics
May 4, 2023 - In the first step, we created a variable x and initialized it with a null value. Inside the if condition, we are simply checking the value of x is null or not by using the equality operator '=='
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-determine-if-variable-is-undefined-or-null-in-javascript.php
How to Determine If Variable is Undefined or NULL in JavaScript
In simple words you can say a null ... if a variable is undefined or null you can use the equality operator == or strict equality operator === (also called identity operator)....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-undefined-value-in-javascript
How to check for "undefined" value in JavaScript ? - GeeksforGeeks
July 23, 2025 - // Using the 'typeof' operator: // Declare a variable let fruit; // Condition for check variable is defined or not if (typeof fruit === "undefined") { console.log("fruit is undefined"); } else { console.log("fruit is defined"); }
🌐
Medium
medium.com › deno-the-complete-reference › five-ways-to-check-for-undefined-in-javascript-b5568090df77
Five ways to check for undefined in JavaScript | Tech Tonic
March 10, 2024 - Introduced in ES11, the nullish coalescing operator (??) allows you to provide a default value if the left operand is null or undefined. let maybeDefined = undefined; let defaultValue = "default value"; let result = maybeDefined ?? defaultValue; console.log(result); // Output: "default value" The logical AND operator (&&) evaluates to false if the left operand is false or undefined. You can use this in conjunction with a check for another value to ensure both conditions are met.