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 Web Docs
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 composed of objects; document.querySelector() returns null if it doesn't ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › null-in-javascript
Null in JavaScript - GeeksforGeeks
June 5, 2024 - In the first scenario, we create variableOne that creates a new object of Square, and a value of 10 is passed in the create_function() method. In the second scenario, we have created variableTwo but we do not pass anything there and therefore ...
Discussions

In javascript, can a value ever return null if it is not explicitly assigned null?

In JS, undefined is the default value for unassigned values.

null == undefined //yields true
null === undefined //yields false

So, you can do a fuzzy check for null, and it will safely match both null and undefined. But as far as I'm aware, you have to explicitly set null for it to return null.

More on reddit.com
🌐 r/learnprogramming
3
0
October 15, 2019
getAttribute on button is returning null
Showing is not an allowed attribute on a button. If you want to store custom data as element attributes looks into using data-* http://www.w3schools.com/tags/att_global_data.asp More on reddit.com
🌐 r/learnjavascript
12
2
February 7, 2017
Best way to delete undefined values from an object
If you only want to remove the properties that are undefined, but null is ok, then use this: for (let key in person) { if(person[key] === undefined) { delete person[key] } } If you also want to remove properties that are null, then just remove the && person[key] !== null part. More on reddit.com
🌐 r/node
7
3
July 20, 2017
return null ?
For the first part: ngOnInit is not returning null, the arrow function passwordMatcher, which I assume is a form validator, is returning null. I personally prefer to put my validators in a separate function in a separate file, but both are fine. Angular form validators are expected to return null when no errors are found. Apart from that, the formControlNames might be a bit messed up newPassword => email and newPasswordRepeat => confirm. That looks like some ugly copy / paste. So something like: export function generateMatcherValidator(newControlKey: string, confirmControlKey: string) { return (control: AbstractControl): { [key: string]: boolean } => { const newControl = control.get(newControlKey); const confirmControl = control.get(confirmControlKey); if (!newControl || !confirmControl) { return null; } return newControl.value === confirmControl.value ? null : { nomatch: true }; }; } For the second part: I quite often just do a truthy check. Although I do try to keep my code as flat as possible, so instead of wrapping entire functions in an if-condition, I just return if that condition is false. So something like: function randomFunction(someArgumentUsedToCallAfunction) { if (!someArgumentUsedToCallAfunction) { return; } do stuff, because we were called with a value } More on reddit.com
🌐 r/typescript
10
5
February 5, 2021
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;

🌐
JavaScript Tutorial
javascripttutorial.net › home › an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - const square = null; if (square) { console.log('The square is not null'); } else { console.log('The square is null'); }Code language: JavaScript (javascript) ... In this example, the square variable is null therefore the if statement evaluates ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › null
null - JavaScript | MDN
May 23, 2022 - 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 composed of objects; document.querySelector() returns null if it doesn't ...
🌐
Programiz
programiz.com › javascript › null-undefined
JavaScript null and undefined
For example, let name = "Felix"; // assigning undefined to the name variable name = undefined console.log(name); // returns undefined · Note: Usually, null is used to assign 'unknown' or 'empty' value to a variable. Hence, you can assign null to a variable. In JavaScript, null is a special ...
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript - Dmitri Pavlutin
September 21, 2020 - ... If you see null (either assigned ... an object wasn't created. For example, the function greetObject() creates objects, but also can return null when an object cannot be created:...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript - MDN Web Docs
null || undefined ?? "foo"; // raises a SyntaxError true && undefined ?? "foo"; // raises a SyntaxError · Instead, provide parenthesis to explicitly indicate precedence: ... In this example, we will provide default values but keep values other than null or undefined.
🌐
Syncfusion
syncfusion.com › blogs › javascript › null vs. undefined in javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - Since undefined is the default value assigned by JavaScript to uninitialized variables, if you want to indicate the absence of a deal explicitly, always use null instead of undefined to avoid confusion. To check if a variable has any value before proceeding further in a program, you can use the loose equality ==null to check for either null or undefined.For example, in the following program, the function assignVal() checks whether the num is undefined or null and assigns the value given by the user only if the variable num is not initialized to any 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 - If the value is null then returns true otherwise it returns false. ... Example: In this example, we are checking whether the given value is null or not by the use of the _isNull() method.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › null
`null` in JavaScript - Mastering JS
December 2, 2020 - The primary difference is purely semantic: undefined means the variable has not been assigned a value yet, whereas null means the variable has been explicitly defined as null. For most practical purposes, null and undefined are often interchangeable as the only two nullish values.
🌐
Favtutor
favtutor.com › articles › null-javascript
Check for Null in JavaScript | 3 Easy Methods (with code)
January 5, 2024 - In this example, we use the typeof operator to check if the type of variable is an Object and it holds a null value. If both conditions are satisfied, then the variable holds a null value.
🌐
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 values without causing runtime errors.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-check-for-null-values-in-javascript
How do I check for null values in JavaScript?
July 22, 2022 - The below example demonstrates how to use the strict equality operator (===) to check for null values in JavaScript.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
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. 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 »
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-null-and-undefined
Difference between null and undefined in JavaScript
A null means the absence of a value. You assign a null to a variable with the intention that currently this variable does not have any value but it will have later on. It is like a placeholder for a value.
🌐
Medium
vvkchandra.medium.com › essential-javascript-mastering-null-vs-undefined-66f62c65d16b
Essential JavaScript: Mastering null vs undefined | by Chandra Gundamaraju | Medium
September 9, 2020 - Continue reading to find the answer! ... value. ... In this example, we intentionally indicate that the universe exists, but it is initialized “explicitly” with an empty value represented by null....
🌐
The Trevor Harmon
thetrevorharmon.com › blog › loose-null-checks-in-javascript
Loose null checks in JavaScript | The Trevor Harmon
June 3, 2024 - I have observed that JavaScript ... indicates the intentional absence of a value. For example, in a getter function, you might return null if the value is not found....
🌐
Colt Steele
coltsteele.com › tips › comparing-null-and-undefined-in-javascript
Comparing null and undefined in JavaScript | Colt Steele
August 23, 2023 - If you're trying to determine whether ... should always compare to null with ===. For example audioInputDevice === null will only be true if audioInputDevice is actually null....