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
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
Summary: JavaScript offers several ways to check for null, including strict (===) and loose (==) equality, Object.is() and boolean coercion. Developers often use typeof and optional chaining (?.) to safely identify null, undefined or undeclared ...
Published ย  August 4, 2025
People also ask

Is null false in JavaScript?
Null is not considered false in JavaScript, but it is considered falsy. This means that null is treated as if itโ€™s false when viewed through boolean logic. However, this is not the same thing as saying null is false or untrue.
๐ŸŒ
builtin.com
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
What is a null check?
In JavaScript, null represents an intentional absence of a value, indicating that a variable has been declared with a null value on purpose. On the other hand, undefined represents the absence of any object value that is unintentional. A null check determines whether a variable has a null value, meaning a valid instance of a type exists.
๐ŸŒ
builtin.com
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
What is a strict null check?
StrictNullChecks is a feature that treats null and undefined as two separate types, reducing errors and making it easier to find coding bugs. It also has stronger measures for defining variables as null or undefined, ensuring variables are declared as null only when itโ€™s safe to do so.
๐ŸŒ
builtin.com
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ check-undefined-null
JavaScript Program To Check If A Variable Is undefined or null
In the above program, a variable is checked if it is equivalent to null. The null with == checks for both null and undefined values.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ null
null - JavaScript | MDN
function getVowels(str) { const m = str.match(/[aeiou]/gi); if (m === null) { return 0; } return m.length; } console.log(getVowels("sky")); // Expected output: 0 ... The keyword null is a literal for the null value. Unlike undefined, which is a global variable, null is not an identifier but a syntax keyword. ... Like undefined, accessing any property on null throws a TypeError instead of returning undefined or searching prototype chains. Like undefined, null is treated as falsy for boolean operations, and nullish for nullish coalescing and optional chaining.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ javascript-check-if-variable-is-a-undefined-or-null
JavaScript: Check if Variable is undefined or null
March 29, 2023 - In this short guide, we've taken a look at how to check if a variable is null, undefined or nil in JavaScript, using the ==, === and typeof operators, noting the pros and cons of each approach.
Top answer
1 of 16
5789

You can just check if the variable has a truthy value or not. That means

if (value) {
    // do something..
}

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string ("")
  • 0
  • false

The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.

Furthermore, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance

if (typeof foo !== 'undefined') {
    // foo could get resolved and it's defined
}

If you can be sure that a variable is declared at least, you should directly check if it has a truthy value like shown above.

2 of 16
457

This question has two interpretations:

Check if the variable has a value
Check if the variable has a truthy value

The following answers both.

In JavaScript, a value could be nullish or not nullish, and a value could be falsy or truthy.
Nullish values are a proper subset of falsy values:

 โ•ญโ”€ nullish โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ•ญโ”€ not nullish โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”
โ”‚ undefined โ”‚ null โ”‚ false โ”‚ 0 โ”‚ "" โ”‚ ... โ”‚ true โ”‚ 1 โ”‚ "hello" โ”‚ ... โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”˜
 โ•ฐโ”€ falsy โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ•ฐโ”€ truthy โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Check if value is nullish (undefined or null)

Use one of the following depending on your coding style:

if (value == null)                         { /* value is nullish */ }
if (value === undefined || value === null) { /* value is nullish */ }
if (value == undefined)                    { /* value is nullish */ }
if ((value ?? null) === null)              { /* value is nullish */ }

Notes:

  • The == operator works because it has a special case for null vs undefined comparison
  • The === operator is more readable (opinion based), eqeqeq friendly and allows checking for undefined and null separately
  • The first and third examples work identically, however the third one is rarely seen in production code
  • The fourth example uses nullish coalescing operator to change nullish values to null for straight forward comparison

Check if value is not nullish

if (value != null)                         { /* value is not nullish, although it could be falsy */ }
if (value !== undefined && value !== null) { /* value is not nullish, although it could be falsy */ }
if (value != undefined)                    { /* value is not nullish, although it could be falsy */ }
if ((value ?? null) !== null)              { /* value is not nullish, although it could be falsy */ }

Check if value is falsy

Use the ! operator:

if (!value) { /* value is falsy */ }

Check if value is truthy

if (value) { /* value is truthy */ }

Data validation

The nullish, falsy and truthy checks cannot be used for data validation on their own. For example, 0 (falsy) is valid age of a person and -1 (truthy) is not. Additional logic needs to be added on case-by-case basis. Some examples:

/*
 * check if value is greater than/equal to 0
 * note that we cannot use truthy check here because 0 must be allowed
 */
[null, -1, 0, 1].forEach(num => {
  if (num != null && num >= 0) {
    console.log("%o is not nullish and greater than/equal to 0", num);
  } else {
    console.log("%o is bad", num);
  }
});

/*
 * check if value is not empty-or-whitespace string
 */
[null, "", " ", "hello"].forEach(str => {
  if (str && /\S/.test(str)) {
    console.log("%o is truthy and has non-whitespace characters", str);
  } else {
    console.log("%o is bad", str);
  }
});

/*
 * check if value is not an empty array
 * check for truthy before checking the length property
 */
[null, [], [1]].forEach(arr => {
  if (arr && arr.length) {
    console.log("%o is truthy and has one or more items", arr);
  } else {
    console.log("%o is bad", arr);
  }
});

/*
 * check if value is not an empty array
 * using optional chaining operator to make sure that the value is not nullish
 */
[null, [], [1]].forEach(arr => {
  if (arr?.length) {
    console.log("%o is not nullish and has one or more items", arr);
  } else {
    console.log("%o is bad", arr);
  }
});
Find elsewhere
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Boolean
Boolean - JavaScript | MDN
However, when comparing with false, which is a primitive, [] is also converted to a primitive, which is "" via Array.prototype.toString(). Comparing strings and booleans results in both being converted to numbers, and they both become 0, so [] == false is true. In general, falsiness and == false differ in the following cases: NaN, undefined, and null are falsy but not loosely equal to false.
๐ŸŒ
STechies
stechies.com โ€บ check-variable-undefined-null
How to Check if the Variable is undefined or null in Javascript
As discussed above, users can check if a variable is null or undefined using the OR (||) operator. It checks if the variable satisfies either of the two conditions. When users use it with two Boolean values, the OR operator will return a "true" ...
๐ŸŒ
Oida
oida.dev โ€บ boolean-in-javascript-and-typescript
Boolean in JavaScript and TypeScript
boolean is a fun primitive data type in JavaScript. In TypeScript, it allows for a total of four values Wait, four? boolean can take the values of true and false. Values from other types can be truthy or falsy, like undefined or null. let b = true if(b) console.log('logged') b = false if(b) console.log('not logged') b = undefined if(b) console.log('not logged') b = null if(b) console.log('not logged')
๐ŸŒ
web.dev
web.dev โ€บ learn โ€บ javascript โ€บ data-types โ€บ null-undefined
null and undefined | web.dev
The loose equality operator coerces operands of different types to boolean values, making null and undefined both false. The strict equality operator considers operands of different data types to be unequal. null == undefined > true null === undefined > false ยท Unlike the reserved keyword null, undefined is a property of the global object. This was a design decision made early in JavaScript's development, and it let legacy browsers overwrite undefined completely.
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ javascript
How to check if value is undefined or null in JavaScript
June 8, 2023 - In this blog post, we have explored various ways to check if a value is undefined or null in JavaScript, as well as gained a deeper understanding of these often misunderstood concepts. By using the methods discussed in this post, you can confidently handle undefined and null values in your JavaScript code.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_booleans.asp
JavaScript Booleans
JavaScript Loops ยท You can use the Boolean() function to find out if an expression (or a variable) is true: Boolean(10 > 9) Try it Yourself ยป ยท Or even easier: (10 > 9) Try it Yourself ยป ยท 100 is true 3.14 is true -15 is true true is true ...
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ check-if-a-value-is-null-or-undefined-in-javascript-or-node-js
Check if a Value Is Null or Undefined in JavaScript or Node.js
/** * Determine whether the given `value` is `null` or `undefined`. * * @param {*} value * * @returns {Boolean} */ function isNullOrUndefined (value) { return value == null // `value == null` is the same as `value === undefined || value === null` } The value == null comparison is a handy way to check for null and undefined simultaneously. The loose equality operator attempts to convert and compare operands of different types. Iโ€™m the maintainer of the @supercharge/goodies package providing convenient helper functions for Node.js and JavaScript.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-nullable-how-to-check-for-null-in-js
JavaScript Nullable โ€“ How to Check for Null in JS
July 7, 2022 - null === undefined evaluates as false because they are not, in fact, equal. <null_variable> === null is the best way to strictly check for null. Object.is(<null_variable>,null) is an equally reliable way to check for null.
๐ŸŒ
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
<script> var firstName; var lastName = null; // Try to get non existing DOM element var comment = document.getElementById('comment'); console.log(firstName); // Print: undefined console.log(lastName); // Print: null console.log(comment); // Print: null console.log(typeof firstName); // Print: undefined console.log(typeof lastName); // Print: object console.log(typeof comment); // Print: object console.log(null == undefined) // Print: true console.log(null === undefined) // Print: false /* Since null == undefined is true, the following statements will catch both null and undefined */ if(firstNa
๐ŸŒ
Codidact
software.codidact.com โ€บ posts โ€บ 291489 โ€บ 291490
In javascript is there really a good reason to never check for boolean true || false such as if(var){}else{}? - Software Development
By this rule, if(var) is completely fine so long as you know var is a boolean value. This rule suggests that you should almost never use == and should instead prefer === (similarly for != and !==). This rule implies that null and undefined should be treated as distinct values from true and false and should be tested for more explicitly.