I don't think you can make that any simpler, but you could certainly refactor that logic into a function:

function isRealValue(obj)
{
 return obj && obj !== 'null' && obj !== 'undefined';
}

Then, at least your code becomes:

if (isRealValue(yourObject))
{
 doSomething();
}
Answer from aquinas on Stack Overflow
🌐
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 - You can check for null with the typeof() operator in JavaScript. console.log(typeof(leviticus)) // object console.log(typeof(dune)) // undefined · Curiously, if you check with typeof(), a null variable will return object.
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
🌐
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.
Published   August 4, 2025
🌐
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
September 11, 2016 -

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.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - let maybeNull = null // The following is equivalent to // maybeNull == null // or maybeNull == undefined: console.log(Object.is(maybeNull, undefined) || Object.is(maybeNull, null)) // Compare to the following: console.log(maybeNull == null) console.log(maybeNull == undefined) console.log(maybeNull === null) console.log(Object.is(maybeNull, null)) console.log(maybeNull === undefined) console.log(Object.is(maybeNull, undefined)) maybeNull = undefined console.log(maybeNull === undefined || maybeNull === null) console.log(maybeNull == null) console.log(maybeNull == undefined) console.log(maybeNull
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩. ... 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
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-for-null-in-javascript
JS Check for Null – Null Checking in JavaScript Explained
November 7, 2024 - let userName = null; console.log(typeof(userName)); // object · 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. Null and undefined are very similar in JavaScript and are both primitive types. A variable has the type of null if it intentionally contains the value of null. In contrast, a variable has the type of undefined when you declare it without initiating a value. // This is null let firstName = null; // This is undefined let lastName;
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-check-if-all-object-properties-are-null
Check if all Object Properties are Null in JavaScript | bobbyhadz
If the every() method returns true, then all of the object's values are null. You can also check if the object's values are set to null, undefined or empty string, or any other value that your use case requires.
🌐
GitConnected
levelup.gitconnected.com › how-to-check-for-an-object-in-javascript-object-null-check-3b2632330296
How to Check for an Object in Javascript (Object Null Check) | by Dr. Derek Austin 🥳 | Level Up Coding
January 5, 2023 - Typically, you’ll check for null using the triple equality operator (=== or !==), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null.
🌐
TutorialsPoint
tutorialspoint.com › How-do-I-check-for-null-values-in-JavaScript
How do I check for null values in JavaScript?
In this tutorial, we used three approaches to check null values in JavaScript. In the first approach, we used the strict equality operator and showed that two null values could be compared. The second approach is beneficial as it uses the Object.is() function to check and compare two null values.
🌐
SamanthaMing
samanthaming.com › tidbits › 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
// TypeError: Cannot covert undefined or null ot object goodEmptyCheck(undefined); goodEmptyCheck(null); If you don't want it to throw a TypeError, you can add an extra check:
🌐
Scaler
scaler.com › topics › javascript-check-null
JavaScript Program to Check for Null - Scaler Topics
December 14, 2022 - Here we will use the !variable because applying ! in front of a null object will make the object not null. So, the if condition will become true. ... In the first step, we created a variable x and initialized it with a null value. Inside the if condition, we have used the typeof() method to check the data type of x. The condition will be true if the value of typeof(x) will return true and the value of !x is also true.
🌐
GitHub
gist.github.com › DoctorDerek › 324485b20b9fffa9648dd17bb374d189
How to Check for an Object (Javascript Object Null Check) · GitHub
How to Check for an Object (Javascript Object Null Check) - How to Check for an Object (Javascript Object Null Check).js
🌐
JavaScript Tutorial
javascripttutorial.net › home › an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - const rect = null; const square = { dimension: 10 }; console.log(rect === null); // true console.log(square === null); // falseCode language: JavaScript (javascript) The rect === null evaluates to true because the rect variable is assigned to a null value. On the other hand, the square === null evaluates to false because it’s assigned to an object. To check if a value is not null, you use the strict inequality operator (!==):
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript
The good way to check for null is by using the strict equality operator: ... If the variable contains a non-null value, like an object, the expression existingObject === null evaluates to false. null, alongside false, 0, '', undefined, NaN, ...
🌐
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 - Example: In this example we are using typeof operator with !== undefined and !== null checks if a variable is not null in JavaScript.
🌐
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