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.

Answer from jAndy on Stack Overflow
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);
  }
});
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-for-null-undefined-or-blank-variables-in-javascript
How to check for null, undefined or blank Variables in JavaScript ? - GeeksforGeeks
May 7, 2023 - ... Example: Here we will check the value of our variable with the help of the triple equals (===) operator. ... Blank variable: Blank variable is one that has been assigned a value, but that value is an empty ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-check-empty-string-checking-null-or-empty-in-js
JavaScript Check Empty String โ€“ Checking Null or Empty in JS
November 7, 2024 - In this first method, we will check for the length of the string by adding the length property. We'll check if the length is equal to 0. If itโ€™s equal to zero, it means that the string is empty, as we can see below:
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ test for empty values in javascript
Test for Empty Values in Javascript โ€” SitePoint
November 6, 2024 - However, our JavaScript function is far more precise about what kinds of data can be considered empty: ... Booleans and numbers are never empty, irrespective of their value. ... const empty = (data) => { // Check if data is a number or boolean, and return false as they're never considered empty if (typeof data === 'number' || typeof data === 'boolean') { return false; } // Check if data is undefined or null, and return true as they're considered empty if (typeof data === 'undefined' || data === null) { return true; } // Check if data has a length property (e.g.
๐ŸŒ
Medium
medium.com โ€บ @tempmailwithpassword โ€บ javascript-checking-for-null-empty-or-undefined-variables-9da87dfc9c8c
JavaScript Checking for Null, Empty, or Undefined Variables
August 24, 2024 - JavaScript does not have a built-in method specifically for checking if an object is empty, but you can use Object.keys(obj).length === 0 to determine if an object has no own properties.
๐ŸŒ
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 - But because we have both undefined and null to cater to, and both can mean different things, whenever we perform a check for null, we need to think about exactly what kind of check we are trying to perform and act accordingly. Because null is a โ€œfalseyโ€ value, it can be tempting to write code like if (!value) to do something if a variable is null. But, as weโ€™ve seen, that can also permit empty strings and empty arrays to slip through that check.
Find elsewhere
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
You can use the logical OR (||) operator if you want to explicitly check if a variable stores an empty string, undefined or null.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-check-for-null-undefined-or-blank-variables-in-javascript
How to check for null, undefined, or blank variables in JavaScript?
In the first variable, we have assigned the null value, and in another variable, we haven't assigned the value. We are checking if it is null or not using the above method, and users can see the result in the output. <html> <body> <h2>Check for variable is undefined or null using <i>strict equality operator</i></h2> <h4>output for let var1 = null;</h4> <div id = "output1"></div> <h4>output for let var1;</h4> <div id = "output2"> </div> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); function checkVar( variable ) { if (variable == null || variable === 'undefined') { return " varaible is undefined or null; " } else { return " variable is properly defined and its value is " + variable; } } let var1 = null; let var2; output1.innerHTML = checkVar(var1); output2.innerHTML = checkVar(var2); </script> </body> </html>
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
This works because if one of the ... of the logical OR (||) operator is evaluated. To check for an empty string, the logical && operator is used....
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ how-do-i-check-for-an-empty-undefined-null-string-in-javascript
how do i check for an empty undefined null string in javascript
April 12, 2024 - To ensure our application is robust and free from unexpected errors, it's important to handle cases where a string might be undefined or null. Considering JavaScript's type coercion and truthy/falsy evaluation, a more encompassing check can be performed to cover empty, undefined, and null strings in a single condition. let myString; if (!myString) { console.log("The string is empty, undefined, or null"); } else { console.log("The string has content"); }
๐ŸŒ
Medium
codehome.medium.com โ€บ how-do-i-check-for-an-empty-undefined-null-string-in-javascript-b4a9507742b4
How do I check for an empty/undefined/null string in JavaScript? | by Programise | Medium
January 16, 2023 - let variable; if(typeof variable === "undefined") { console.log("Variable is undefined"); } 3. To check for a null value, you can use the comparison operator == or ===. Both null and undefined are falsy values, so you can use ! operator to check. ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-if-a-variable-is-not-null-in-javascript
How to check if a Variable Is Not Null in JavaScript ? - GeeksforGeeks
August 5, 2025 - In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-string-is-empty-or-null-javascript
How to Check if a String is Empty or Null in JavaScript โ€“ JS Tutorial
November 7, 2024 - In this example, we're first using the trim method to remove any leading or trailing whitespace characters from the str variable, then checking whether the resulting string has zero length. If it does, then we know that the string is empty. Otherwise, we know that the string is not empty. Here are some best practices to follow when checking for empty or null strings in JavaScript:
๐ŸŒ
SmartBear Community
community.smartbear.com โ€บ smartbear community โ€บ testcomplete โ€บ testcomplete questions
[JS SCRIPT] Best way to determine if variable is empty | SmartBear Community
// Return true if variable is undefined or empty // Works only on object of one level // For standard object variable (string, table, number, ..) use isEmptyVar() function isEmptyObject(obj) { return (typeof obj == 'undefined') || (Object.keys(obj).length === 0 && obj.constructor === Object); ...
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ how-to-check-for-empty-undefined-or-null-strings-in-javascript-d8f0bf514ead
How to Use JavaScript to Check for Null, Empty, or Undefined Strings
August 24, 2024 - In the first script, we create a function called isStringEmpty that accepts a single parameter, value. This function returns true if the value is either undefined, null, or an empty string (โ€œโ€).
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
It's just regular, plain JavaScript without the use of a library like Lodash or jQuery. We can use the built-in Object.keys method to check for an empty object.
๐ŸŒ
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.