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 ...
๐ŸŒ
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.
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;

๐ŸŒ
web.dev
web.dev โ€บ learn โ€บ javascript โ€บ data-types โ€บ null-undefined
null and undefined | web.dev
This page describes the two most common ways: the null and undefined data types. The null keyword represents an intentionally defined absence of value. null is a primitive, although the typeof operator returns that null is an object.
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ null
`null` in JavaScript - Mastering JS
In JavaScript, null is a value that represents the intentional absence of any object value. It is technically a primitive type, although in some cases it behaves as an object.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ null-undefined
JavaScript null and undefined
Note: null is not the same as NULL or Null. In JavaScript, undefined and null are treated as false values.
๐ŸŒ
Dmitri Pavlutin
dmitripavlutin.com โ€บ javascript-null
Everything about null in JavaScript
null in JavaScript is a special value that represents a missing object.
Find elsewhere
๐ŸŒ
Luis Llamas
luisllamas.es โ€บ inicio โ€บ cursos โ€บ curso javascript
What is null and undefined in JavaScript and how to use them
December 6, 2024 - That is to say, as we see, the difference between the two is that undefined is automatic, and null is explicit. undefined is a primitive value in JavaScript.
๐ŸŒ
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.

๐ŸŒ
The Trevor Harmon
thetrevorharmon.com โ€บ blog โ€บ loose-null-checks-in-javascript
Loose null checks in JavaScript | The Trevor Harmon
I have observed that JavaScript ... this, like in the exec() method of the regular expression object). null indicates the intentional absence of a value....
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
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.
Published ย  August 4, 2025
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 142857-understanding-the-values-null-and-undefined-in-javascript
[JavaScript] - Understanding the values null and undefined in JavaScript
Black Friday Sale ๐ŸŽ‰ 30% off on ... 1st Ending in 5 days Get Deal Get This Deal NOW ... Learn the meaning of null and undefined in JavaScript and how they are used to represent the absence of a value. ... Write a function that takes in a single number. It should return the string even if the number is even, and ...
๐ŸŒ
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
javascript ยท Wed Nov 08 2023 ยท Day 8 of National Blog Posting Month #NaBloPoMo ยท In JS, itโ€™s easy to get caught out when determining if a variable is null, undefined, or has a value of zero. I do a lot of data visualisation, and quite often Iโ€™ll need to filter out null values from an array of data.
๐ŸŒ
Tamalweb
tamalweb.com โ€บ null-javascript
What is null in JavaScript? | TamalWeb by Tamal Chowdhury
March 19, 2020 - Every programming language has at least one bottom value but JavaScript has two: null and undefined (Crockford, 2017). We are going to focus on null for this article because there is so much thatโ€™s going on behind the scenes.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is because a variable that has been declared but not assigned any value is undefined, not null. ... By this operator, we will learn how to check for null values in JavaScript by the (===) operator.
๐ŸŒ
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 - Using the typeof operator, JavaScript checks if a variable is not null by verifying typeof variable !== 'undefined' && variable !== null. This approach ensures the variable exists and is not explicitly set to null, promoting reliable code execution.
๐ŸŒ
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.
๐ŸŒ
Harness
harness.io โ€บ blog โ€บ feature management & experimentation โ€บ why you should use undefined instead of null in javascript
Why You Should Use Undefined Instead of Null in Javascript | Blog
1 month ago - The blog outlines best practices and edge cases, concluding that `undefined` provides cleaner and more predictable results. JavaScript is an unusual programming language in that it has two null-like values: undefined and null.