They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null.

The only values that are not truthy in JavaScript are the following (a.k.a. falsy values):

  • null
  • undefined
  • 0
  • "" (the empty string)
  • false
  • NaN
Answer from Tim Down on Stack Overflow
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
The value null is falsy, but empty objects are truthy, so typeof maybeNull === "object" && !maybeNull is an easy way to check to see that a value is not null. Finally, to check if a value has been declared and assigned a value that is neither ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript - MDN Web Docs
If you are designing an API, you should likely accept null and undefined as equivalent inputs, because many codebases have stylistic rules about when to use null or undefined by default. When checking for null or undefined, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion. ... typeof null; // "object" (not "null" for legacy reasons) typeof undefined; // "undefined" null === undefined; // false null == undefined; // true null === null; // true null == null; // true !null; // true Number.isNaN(1 + null); // false Number.isNaN(1 + undefined); // true
Discussions

javascript - How to check if a variable is not null? - Stack Overflow
I know that below are the two ways in JavaScript to check whether a variable is not null, but I’m confused which is the best practice to use. ... They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. More on stackoverflow.com
🌐 stackoverflow.com
Good way to check for variable being not null and not undefined.
There are some style guides that basically say you always should use ===, but you can use == in order to check for null or undefined at the same time. So you could do the following: if (value != null) { // This will run if `value` is not `null` and not `undefined`. } More on reddit.com
🌐 r/javascript
56
32
October 20, 2016
javascript check for not null - Stack Overflow
should be if (null != val) somehow it was not working with the quotes 2016-08-15T09:09:28.293Z+00:00 ... Mr.x in the question, null was as string ...your case is probably different 2016-08-15T13:26:24.73Z+00:00 ... Use !== as != will get you into a world of nontransitive JavaScript truth table weirdness. ... Unless you want to specifically check ... More on stackoverflow.com
🌐 stackoverflow.com
How do I check for null values in JavaScript? - Stack Overflow
How can I check for null values in JavaScript? I wrote the code below but it didn't work. if (pass == null || cpass == null || email == null || cemail == null || user == null) { alert("fill ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-for-null-in-javascript
JS Check for Null – Null Checking in JavaScript Explained
November 7, 2024 - But, this can be tricky because if the variable is undefined, it will also return true because both null and undefined are loosely equal. let firstName = null; let lastName; console.log(firstName == null); // true console.log(lastName == null); // true console.log(firstName == undefined); // true console.log(lastName == undefined); // true console.log(firstName == lastName); // true console.log(null == undefined); // true · Note: This can be useful when you want to check if a variable has no value because when a variable has no value, it can either be null or undefined.
Find elsewhere
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
732

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;

🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-check-if-variable-is-not-null
Check if a Variable is Not NULL in JavaScript | bobbyhadz
Copied!const a = 'hello'; if (a) { console.log(`🚨 a is NOT false, 0, empty string, null, undefined, NaN`); } else { console.log(`⛔️️ a is ONE OF false, 0, empty string, null, undefined, NaN`); } In this example, we check if the value stored in the variable a is truthy. Truthy are all values that are not falsy. The falsy values in JavaScript are: false, 0, "", null, undefined, NaN.
🌐
TutorialsPoint
tutorialspoint.com › How-do-I-check-for-null-values-in-JavaScript
JavaScript - Null Checking
July 22, 2022 - In this example, we use the JavaScript typeof operator to check whether the values of a given variable are equal to null or not. <!DOCTYPE html> <html> <head> <title>Check for null values in JavaScript</title> </head> <body> <h2>Check for null ...
🌐
Medium
medium.com › programming-essentials › how-to-check-if-a-property-is-not-null-7d0312ca9d7b
How to Check If a Property Is Not Null | by Cristian Salcescu | Frontend Essentials | Medium
April 19, 2021 - Let’s start by just checking that a property is not null. const book = { author : null };if (book.author !== null){ console.log('not null'); }
🌐
Futurestud.io
futurestud.io › tutorials › check-if-a-value-is-null-or-undefined-in-javascript-or-node-js
Check if a Value Is Null or Undefined in JavaScript or Node.js
August 4, 2022 - In such cases, you’re running into errors where you’re not able to access a property or method on “null” or “undefined”. This tutorial shows you how to check whether a given value is either null or undefined. ... You can create a utility method checking whether a given input is null and undefined. Here’s a sample function that you may copy to your project and is it to check for empty values: /** * Determine whether the given `value` is `null` or `undefined`. * * @param {*} value * * @returns {Boolean} */ function isNullOrUndefined (value) { return value == null // `value == null` is the same as `value === undefined || value === null` }
🌐
Favtutor
favtutor.com › articles › null-javascript
Check for Null in JavaScript | 3 Easy Methods (with code)
January 5, 2024 - If the variable holds a null value, ... the data type of a variable. We can combine the typeof operator and a conditional statement to check if a variable holds a null value....
🌐
Scaler
scaler.com › home › topics › javascript program to check for null
JavaScript Program to Check for Null - Scaler Topics
May 4, 2023 - In this approach, we will use the strict equality operator '===' to check if the variable contains null or not.
🌐
CoreUI
coreui.io › answers › how-to-check-if-a-variable-is-null-in-javascript
How to check if a variable is null in JavaScript · CoreUI
November 6, 2025 - Here value === null uses strict equality to check if the variable is exactly null. This approach distinguishes null from other falsy values like undefined, 0, false, or empty strings.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? | GeeksforGeeks
August 28, 2024 - In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized.
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › check-if-string-is-empty-or-null-javascript
How to Check if a String is Empty or Null in JavaScript – JS Tutorial
November 7, 2024 - In this example, we're first using the trim method to remove any leading or trailing whitespace characters from the str variable, then checking whether the resulting string has zero length. If it does, then we know that the string is empty. Otherwise, we know that the string is not empty. Here are some best practices to follow when checking for empty or null strings in JavaScript:
🌐
DEV Community
dev.to › wolfhoundjesse › null-checking-in-javascript-lc4
Null-checking in JavaScript - DEV Community
April 11, 2019 - What are your thoughts? How are you checking for empty or null values? Bonus if you comment with examples in other programming languages! ... It's more a bright exemple of how to make simple things looks complicated because in javascript if(tokenInfo) will check if variable is either :
🌐
LogRocket
blog.logrocket.com › home › how to check for null, undefined, or empty values in javascript
How to check for null, undefined, or empty values in JavaScript - LogRocket Blog
February 14, 2025 - The box does not exist. Most of the time, checking that an item is null will be enough. But because we have both undefined and null to cater to, and both can mean different things, whenever we perform a check for null, we need to think about exactly what kind of check we are trying to perform and act accordingly. Because null is a “falsey” value, it can be tempting to write code like if (!value) to do something if a variable is null.