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
5790

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
466

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);
  }
});
Run code snippetEdit code snippet Hide Results Copy to answer Expand

๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ null
null - JavaScript | MDN
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. The typeof null result is "object". This is a bug in JavaScript that cannot be fixed due to backward compatibility.
Discussions

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
Undefined vs null
Hello. What is the difference between undefined and null? More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
8
0
November 19, 2019
null vs undefined
I try to think of it simply as: undefined means exactly that, we have not bothered to set a value for this thing. null means we explicitly set a value and wanted it to be anomalous. null shows intentionality you cannot infer from undefined. More on reddit.com
๐ŸŒ r/javascript
43
133
January 13, 2018
Not sure when to use Undefined and Null in JavaScript?
When the typeof keyword is used on undefined it outputs "undefined" which is correct. When typeof is used on null it outputs "object "which is not correct and is a flaw in the language that was never fixed. Therefore it is a perfectly acceptable practice to simply never use null and always use undefined. You can use null if you want to but there is no reason to. Here is a reference: https://www.youtube.com/watch?v=vJKDh4UEXhw&feature=youtu.be&t=38m60s More on reddit.com
๐ŸŒ r/learnjavascript
5
2
January 22, 2017
๐ŸŒ
web.dev
web.dev โ€บ learn โ€บ javascript โ€บ data-types โ€บ null-undefined
null and undefined | web.dev
This is an error that has carried ... of JavaScript and been left intentionally unaddressed to avoid breaking expected behavior across the web. ... You might define a variable as null with the expectation that it reflects either a value assigned to it at some point in a script or an explicitly absent value. You can also assign the null value to an existing reference to clear a previous value. undefined is a primitive ...
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ basic js question: when to check for undefined, null, etc
r/javascript on Reddit: Basic JS question: when to check for undefined, null, etc
December 20, 2017 -

So I'm usually more of a server side developer, but lately I've been working with more of the client code at work. I understand what undefined and null are in JavaScript, but I find myself always checking for both of them. In fact, when checking if a String property exists, I end up writing this:

if(value !== undefined && value !== null && value !== '')

I figure there is a better way than this, and it's probably because I'm not 100% clear of when to check for what. So if someone could help fill me in here on the rules of when to check for undefined vs null, that would be great.

Top answer
1 of 5
28

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 (!=).

2 of 5
16

I would just say

if (value) {
  // do stuff
}

because

'' || false
// false
null || false
// false
undefined || false
//false

Edit:

Based on this statement

I end up writing this: if(value !== undefined && value !== null && value !== '')

I initially assumed that what OP was really looking for was a better way to ask "is there a value?", but...

if someone could help fill me in here on the rules of when to check for undefined vs null, that would be great.

If you're looking to see if something is "truthy":

if (foo.bar) {
  alert(foo.bar)
}

This won't alert if value is '', 0, false, null, or undefined

If you want to make sure something is a String so you can use string methods:

if (typeof foo.bar === 'string') {
  alert(foo.bar.charAt(0))
}

This won't alert unless value is of type 'string'.

So.. "when to check for undefined vs null"? I would just say, whenever you know that you specifically need to check for them. If you know that you want to do something different when a value is null vs when a value is undefined, then you can check for the difference. But if you're just looking for "truthy" then you don't need to.

๐ŸŒ
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 - Use value == null to check for both null and undefined. ... String validation ensures data integrity and prevents errors in your application. ... Ensuring that strings are properly validated in JavaScript is crucial for maintaining robust and ...
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ beginners-guide-how-to-check-null-and-undefined-in-typescript-c6492a07b609
Beginnerโ€™s Guide: How to Check Null and Undefined in TypeScript | by Brandon Evans | JavaScript in Plain English
July 17, 2024 - To prevent the runtime error, we use the โ€œ!โ€ operator to assert that the variable is not null or undefined. However, TypeScriptโ€™s type checker still raises an error because it doesnโ€™t know if the variable is null or undefined at runtime.
Find elsewhere
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Undefined vs null - JavaScript - The freeCodeCamp Forum
November 19, 2019 - Hello. What is the difference between undefined and 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
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)....
๐ŸŒ
DEV Community
dev.to โ€บ benjaminmock โ€บ how-to-check-if-a-variable-is-undefined-in-js-3g02
๐Ÿ’ก How to check if a variable is undefined in JS - DEV Community
January 22, 2020 - Here we don't need to check for typeof user.address !== undefined or user.address !== undefined && user.address !== null && typeof user.address == 'string It is simply enough to check for a truthy value. so if(user.address) render() is doing the job just fine.
๐ŸŒ
Negabaro`s Blog
negabaro.github.io โ€บ archive โ€บ js-isEmpty
javascript์˜ null,undefined,empty ์ฒดํฌ์‹œ ์ข‹์€ ์ฝ”๋”ฉ๋ฐฉ๋ฒ• - Negabaro`s Blog
November 17, 2019 - ์ด ๋ถ€๋ถ„์€ ์กฐ์‹ฌํ•ด์•ผํ•˜๋Š”๊ฒŒ 0, โ€œโ€, null, undefined๋„ false์ด๋ฏ€๋กœ ๋‹จ์ˆœํžˆ boolean๊ฐ’์˜ false๋งŒ ํŒ์ •ํ•˜๊ณ  ์‹ถ์„๋•Œ๋Š” if ( foo === false ) ๋กœ ์ ์–ด์ฃผ๋Š”๊ฒŒ ๋งž๋‹ค.
๐ŸŒ
Medium
medium.com โ€บ @yi_yuan โ€บ understanding-null-undefined-and-empty-strings-in-javascript-a07959084d
Understanding Null, Undefined, and Empty Strings in JavaScript | by Yi Yuan | Medium
January 8, 2023 - console.log(favoriteFood); // logs undefined let favoriteFood = "pizza"; Even though JavaScript is a flexible language that does not require you to assign a value immediately when you declare a variable, attempting to use a variable that has not been declared in your entire code will result in a โ€œreference errorโ€. console.log(favoriteFood); // causes a reference error ยท 3. When you try to access a property of an object that does not exist, or use a different case than the one you declared, the result will be undefined:
๐ŸŒ
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.
๐ŸŒ
DEV Community
dev.to โ€บ wolfhoundjesse โ€บ null-checking-in-javascript-lc4
Null-checking in JavaScript - DEV Community
April 11, 2019 - Never use null unless you're trying to express an actual null value. Checking for !variable is usually a sufficient check for undefined unless the variable is boolean. Never use == for comparison unless you have a deep understanding of what it's comparing. If implicit type coercision is confusing, use Typescript. For further actions, you may consider blocking this person and/or reporting abuse
๐ŸŒ
Rampatra
blog.rampatra.com โ€บ null-vs-undefined-in-typescript-or-javascript-how-to-check-for-both-at-once
!== null vs !== undefined in Typescript or Javascript, how to check for both at once?
November 14, 2024 - if (value != null) { // Do something if value is not null or undefined } Prefer strict equality checks (!== or ===) for better type safety and clarity.
๐ŸŒ
W3Schools
w3schools.com โ€บ typescript โ€บ typescript_null.php
TypeScript Null & Undefined
When strictNullChecks is enabled, TypeScript requires values to be set unless undefined is explicitly added to the type. Optional chaining is a JavaScript feature that works well with TypeScript's null handling. It allows accessing properties on an object that may or may not exist, using compact ...
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ js_check_not_null_or_undefined.php
js check not null or undefined: Master JavaScript's Essential Checks for Bug-Free Code (Even You Can Do It!)
To check if a variable is not null or undefined in JavaScript, you can use the loose equality operator (variable != null) for a concise check, or the Nullish Coalescing Operator (??) for assigning a fallback value only when null or undefined.
๐ŸŒ
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 - Now let's say I want it to print "No Data" when there is no actual reading instead of showing undefined or null. Lets try adding a check for falsy values first.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ js_check_is_null.php
Mastering <code>js check is null</code> (2026): Prevent Bugs, Build Robust JavaScript!
The strict equality operator (===) checks both value and type without type coercion, ensuring you are specifically checking for null. == null will also evaluate to true for undefined due to type coercion, which might lead to unintended side effects if you only meant to check for null. Q: How can I provide a default value if a variable is null or undefined in JavaScript?
๐ŸŒ
GitHub
gist.github.com โ€บ the-vishal-kumar โ€บ dad8faf34c103e722ad74484ae8ef0fc
How to check for null in JavaScript ยท GitHub
Checking for null is a common task that every JavaScript developer has to perform at some point or another ยท The typeof keyword returns "object" for null, so that means a little bit more effort is required ... The value null is falsy, but empty objects are truthy, so typeof maybeNull === "object" && !maybeNull is an easy way to check that a value is not null ยท Finally, to check if a value has been declared and assigned a value that is neither null nor undefined, use typeof
๐ŸŒ
BrowserStack
browserstack.com โ€บ home โ€บ guide โ€บ how to check if a variable is undefined in javascript
How to Check if a Variable is Undefined in JavaScript | BrowserStack
February 18, 2025 - In JavaScript, undefined is the default value for variables that have been declared but not initialized. On the other hand, null is an intentional assignment that explicitly indicates the absence of a value. ... This article covers different ways to check if a variable is undefined, helping you write code that handles these situations smoothly.