JavaScript is very flexible with regards to checking for "null" values. I'm guessing you're actually looking for empty strings, in which case this simpler code will work:

if(!pass || !cpass || !email || !cemail || !user){

Which will check for empty strings (""), null, undefined, false and the numbers 0 and NaN.

Please note that if you are specifically checking for numbers, it is a common mistake to miss 0 with this method, and num !== 0 is preferred (or num !== -1 or ~num (hacky code that also checks against -1)) for functions that return -1, e.g. indexOf).

Answer from user578895 on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
October 28, 2025 - Unlike undefined, JSON.stringify() can represent null faithfully. JavaScript is unique to have two nullish values: null and undefined. Semantically, their difference is very minor: undefined represents the absence of a value, while null represents the absence of an object.
Top answer
1 of 16
1093

JavaScript is very flexible with regards to checking for "null" values. I'm guessing you're actually looking for empty strings, in which case this simpler code will work:

if(!pass || !cpass || !email || !cemail || !user){

Which will check for empty strings (""), null, undefined, false and the numbers 0 and NaN.

Please note that if you are specifically checking for numbers, it is a common mistake to miss 0 with this method, and num !== 0 is preferred (or num !== -1 or ~num (hacky code that also checks against -1)) for functions that return -1, e.g. indexOf).

2 of 16
728

To check for null SPECIFICALLY you would use this:

if (variable === null)

This test will ONLY pass for null and will not pass for "", undefined, false, 0, or NaN.

Additionally, I've provided absolute checks for each "false-like" value (one that would return true for !variable).

Note, for some of the absolute checks, you will need to implement use of the absolutely equals: === and typeof.

I've created a JSFiddle here to show all of the individual tests working

Here is the output of each check:

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number

As you can see, it's a little more difficult to test against NaN;

Discussions

Why does JavaScript have both null and undefined?
Null is a defined value, while undefine is not. This link can help explain why null was created in JS: https://flexiple.com/javascript/undefined-vs-null-javascript/ More on reddit.com
🌐 r/JavaScriptTips
2
12
November 11, 2022
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
September 11, 2016
javascript - Why does (false || null) return null, while (null || false) returns false? - Stack Overflow
Why does false || null return a different result than null || false? Can I safely rely on return myVar || false returning false if myVar is either null or false, but true otherwise? All Combinations: More on stackoverflow.com
🌐 stackoverflow.com
[AskJS] JavaScript operators like or ( || ), and ( && ), null coalescing ( ?? ) makes weak comparasion?
The difference between || and ?? is that ?? runs the right part only when the left part is null or undefined whereas || runs the right part when the left part is a falsey value (falsey values include nulland undefined). More on reddit.com
🌐 r/javascript
13
0
April 12, 2024
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
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › null-in-javascript
Null in JavaScript - GeeksforGeeks
June 5, 2024 - In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present.
🌐
Reddit
reddit.com › r/javascripttips › why does javascript have both null and undefined?
r/JavaScriptTips on Reddit: Why does JavaScript have both null and undefined?
November 11, 2022 -

Most programming languages have a single value to indicate the absence of something, which is often called null and is used to represent a variable that has no value associated with it.

But JavaScript is different. Someone who is just starting out with JavaScript or coming from a different language usually finds it hard to understand, why there are two values that indicate absence: null and undefined

Check out the post to learn how these two are different.

🌐
DEV Community
dev.to › wolfhoundjesse › null-checking-in-javascript-lc4
Null-checking in JavaScript - DEV Community
April 11, 2019 - Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript! ... Looking at this block of code... if ( tokenInfo && tokenInfo !== undefined && tokenInfo !== null && tokenInfo !== "" ) { }
Find elsewhere
🌐
DEV Community
dev.to › sduduzog › null-vs-undefined-what-to-choose-what-to-use-11g
null vs undefined? What to choose? What to use? - DEV Community
August 23, 2023 - When a javascript object is being serialized, all undefined properties are discarded, remember 'undefined' means a property is yet to be assigned a value. But null on the other hand is known by JSON as its a valid JSON data type
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
let value: string | undefined | null = null; value = 'hello'; value = undefined; Try it Yourself » · 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.
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
The null keyword represents an intentionally defined absence of value. null is a primitive, although the typeof operator returns that null is an object. This is an error that has carried over from the first version of JavaScript and been left ...
🌐
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.

🌐
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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
Top answer
1 of 5
18

The || operator in JavaScript doesn't necessarily return true or false. It's exact behavior is this:

If the first operand is truthy, it evaluates to the first operand. Otherwise, it evaluates to the second.

This works as expected given two boolean values, but as you have noticed, you can also use it with any other value. In both of your examples, the first operand is falsey. Thus, both expressions evaluate to the second operand.


Note about one way this is used: The behavior of || is often used to create default arguments:

function foo(optionalArg) {
  optionalArg = optionalArg || "default!";
  console.log(optionalArg);
}

foo("test");
foo();

If optionalArg is omitted, its values implicitly becomes undefined. Because undefined is falsey, undefined || "default!" evaluates to "default!". Note that this style of default args can backfire if you pass a falsey value, like 0 or "". It's more robust to explicitly check for undefined. In ECMAScript 6, you can do this with a default value within the parameter list to be more readable:

function foo(optionalArg = "default!") {
  console.log(optionalArg);
}

foo("test");
foo(false);
foo(undefined);
foo();
2 of 5
3

When you have || between two variables the following happens.

Example: a || b

  1. It checks if the a is truthy. If it is then it returns it. If it is not then it checks the next value.
  2. If it hasn't returned a value yet then it returns the next value no matter what.

Another way to write this is:

if (a) {
    return a;
}
return b;

Or:

a ? a : b;
🌐
Programiz
programiz.com › javascript › null-undefined
JavaScript null and undefined
When comparing null and undefined with equal to operator ==, they are considered equal. For example, ... In JavaScript, == compares values by performing type conversion. Both null and undefined return false.
🌐
YouTube
youtube.com › shorts › 1sIj1lkV3lw
Null in JavaScript: Primitive or Object? 🤔🔎 - YouTube
Null in JavaScript: Primitive or Object? 🤔🔎 Join Anjana Vakil to explore JavaScript and gain the core skills to become a professional programmer! Through h...
Published   March 31, 2025
🌐
Medium
medium.com › weekly-webtips › null-and-undefined-in-javascript-d9bc18acdaff
Null and Undefined in Javascript
February 17, 2021 - Quick Note: As mentioned earlier, null and undefined are both primitive values, but interestingly enough, if we test out in typeof , they gave us different result: All other values in Javascript are objects ({}, [], functions…). So, this is generally regarded as a mistake when Javascript is first created.
🌐
Exploring JS
exploringjs.com › js › book › ch_undefined-null.html
The non-values undefined and null • Exploring JavaScript (ES2025 Edition)
Variables with object types are initialized with null. Each primitive type has its own initialization value. For example, int variables are initialized with 0. JavaScript borrowed null and uses it where objects are expected.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
April 23, 2023 - In other words, ?? returns the first argument if it’s not null/undefined.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-for-null-in-javascript
JS Check for Null – Null Checking in JavaScript Explained
November 7, 2024 - Null is a primitive type in JavaScript. 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 ...