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
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. For example, the end of the prototype chain is null because the prototype chain is ...
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;

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 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
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
๐ŸŒ
Medium
medium.com โ€บ javascript-scene โ€บ handling-null-and-undefined-in-javascript-1500c65d51ae
Handling null and undefined in JavaScript | by Eric Elliott | JavaScript Scene | Medium
November 12, 2019 - If the future hasnโ€™t arrived, yet, youโ€™ll need to install @babel/plugin-proposal-optional-chaining and @babel/plugin-proposal-nullish-coalescing-operator. If a function may not return with a value, it might be a good idea to wrap it in an Either. In functional programming, the Either monad is a special abstract data type that allows you to attach two different code paths: a success path, or a fail path. JavaScript has a built-in asynchronous Either monad-ish data type called Promise.
๐ŸŒ
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
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - Besides false, 0, an empty string (''), undefined, NaN, null is a falsy value. It means that JavaScript will coerce null to false in conditionals.
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ typescript โ€บ typescript_null.php
TypeScript Null & Undefined
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 syntax. It can be used with the ?. operator when accessing properties. interface House { sqft: number; yard?: { sqft: number; }; } function printYardSize(house: House) { const yardSize = house.yard?.sqft; if (yardSize === undefined) { console.log('No yard'); } else { console.log(`Yard is ${yardSize} sqft`); } } let home: House = { sqft: 500 }; printYardSize(home); // Prints 'No yard' Try it Yourself ยป
Find elsewhere
๐ŸŒ
Dmitri Pavlutin
dmitripavlutin.com โ€บ javascript-null
Everything about null in JavaScript
Let's use again greetObject() function ... null, a TypeError error is thrown. You can handle null by either using the optional chaining with nullish coalescing:...
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-do-I-check-for-null-values-in-JavaScript
How do I check for null values in JavaScript?
In the above output, the variable is being checked for null and the value is being executed in the if-block that the variable contains a null value. The Object.is() function in JavaScript that compares two values to see whether they are the same. A boolean value indicates if the two parameters in the function have the same value.
๐ŸŒ
CSS { In Real Life }
css-irl.info โ€บ handling-null-undefined-and-zero-values-in-javascript
CSS { In Real Life } | Handling Null, Undefined and Zero Values in JavaScript
That way, daysSinceLastPost will be used, even if the value is 0, while our fallback will be used if it is null or undefined. Put even more simply: In the following example, test1 will evaluate to 'Test 1', while test2 will evaluate to0.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-check-for-null-in-javascript
JS Check for Null โ€“ Null Checking in JavaScript Explained
November 7, 2024 - 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 = null; ...
๐ŸŒ
Qubits & Bytes
qubitsandbytes.co.uk โ€บ javascript โ€บ shorthand-null-handling-in-javascript
Shorthand Null Handling in Javascript โ€“ Qubits & Bytes
February 15, 2025 - Nullish coalescing assignment assigns a value if the current value is either null, or undefined. If the value on the left-hand side is any other value, no assignment takes place. Because of this, it can be used with a variable defined as a const. ...
๐ŸŒ
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 - Object.is(<null_variable>,null) is an equally reliable way to check for null. Take heart! As you've probably gathered, there are a plethora of brain teasers in the JavaScript ecosystem like this.
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ null
`null` in JavaScript - Mastering JS
It is technically a primitive type, although in some cases it behaves as an object. Here's what you need to know about null: You can check whether a value is null using the === operator: if (v === null) { // Handle `null` case here } You may ...
๐ŸŒ
Full Stack Foundations
fullstackfoundations.com โ€บ blog โ€บ javascript error handling, null, and undefined for beginners
JavaScript Error Handling, null, and undefined for Beginners
March 29, 2024 - Navigate JavaScript Error Handling, null, and undefined: Essential tips for beginners to write more reliable and bug-free code. ... Loading course... ... I know, the rest of this lesson looks rather boring, but if you've made it this far, please stick around because understanding error types, NaN, null, and undefined values is super important! A JavaScript error happens when you try to execute JavaScript code that is either invalid or is incapable of handling the values you have given to it.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ 'null' or 'undefined': what should i use if i want to clear the variable from the memory?
r/learnjavascript on Reddit: 'null' or 'undefined': What should I use if I want to clear the variable from the memory?
June 7, 2023 -

Please consider the following:

var myFruits = ['Banana', 'Apple', 'Strawberry'];
// SOME CODING
// SOME CODING
myFruits = undefined; // Is this better?
myFruits = null; // or is this better?

Further question, what is the distinction between the two? Is there any cases where only null is used or undefined is used? Thanks.

Top answer
1 of 3
5
As NateDzMtz says, the memory considerations are the same. null and undefined are unique values and don't involve any references into the heap. In this regard, false would have the same effect. As far as which is convenient for programming, since indexing an object with a key that is not found in the object returns undefined, storing undefined as the value almost simulates absence of the key. Of course, a query can be made to distinguish the case that foo has no key bar from the case where it has the key bar but undefined is stored as the value at that key. But if your design is such that those cases don't have different meanings, it's convenient to stifle slots by putting undefined in them. Note that delete can be inefficient in some engines and they are not required by the standard to make it efficient. I think that the conventional meanings of the special values are, more or less: undefined -- maybe was never initialized; isn't associated to any particular data type. null -- no object, where an object might be expected. NaN -- no number, where a number might be expected. false -- just not true, no other meaning. Note that typeof null is "object", even though you can't index null. typeof undefined is "undefined". typeof NaN is "number", even though NaN explicitly and exactly means "Not a Number"!
2 of 3
5
In my opinion I typically would use null to denote the absence of the variable for purposes of debugging. It helps with identifying that the variable was intentionally set to a null value as to not be confused with the variable not being defined in the first place. Additionally, using null can be useful when you want to explicitly assign a "no value" state to a variable. This can be helpful in scenarios where you want to differentiate between an intentional absence of a value and a variable that has not been assigned any value yet. On the other hand, undefined is often used by JavaScript itself to indicate that a variable has been declared but has not been assigned any value. It is the default value for uninitialized variables. In most cases, you don't need to explicitly set a variable to undefined because JavaScript does it automatically. However, it's worth noting that both null and undefined have similar behaviors when it comes to memory management. Assigning either of them to a variable will release the memory occupied by the previous value and make the variable eligible for garbage collection. In conclusion, while both null and undefined can be used to clear a variable from memory, null is typically preferred when you want to denote an intentional absence of value, while undefined is automatically assigned by JavaScript when a variable is declared but not assigned a value.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - By this operator, we will learn how to check for null values in JavaScript by the (===) operator.
๐ŸŒ
The Trevor Harmon
thetrevorharmon.com โ€บ blog โ€บ loose-null-checks-in-javascript
Loose null checks in JavaScript | The Trevor Harmon
tl;dr: value == null is an easy way to capture both null and undefined in a equality check, and it's the one clear case that a loose equality check is better than a strict equality check.
๐ŸŒ
DEV Community
dev.to โ€บ rakibrahman โ€บ javascript-essentials-handling-null-undefined-and-safely-accessing-data-with-and--5c6b
JavaScript Essentials: Handling Null, Undefined, and Safely Accessing Data with ?? and ?. - DEV Community
February 1, 2025 - Hopefully, this post will help you firm your knowledge of these data types and how to use nullish coalescing operator & optional chaining when dealing with null and undefined values. In JavaScript null means intentional absence of value