🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript - MDN Web Docs
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
🌐
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 ...
🌐
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 ...
🌐
W3Schools Blog
w3schools.blog › home › difference between undefined value and null value
Difference between undefined value and null value - W3schools
April 24, 2018 - Example: var testNum; ... value: A value that is explicitly specified by the keyword “null” is known as a null value. var test= null; console.log(test); // null · Note: null is of the type Object. var test= null; console.log(typeof test); // object · What is ...
🌐
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 ...
🌐
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 »
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › null
`null` in JavaScript - Mastering JS
December 2, 2020 - Using double equals with null is a shorthand for checking whether a value is null or undefined (so-called nullish values). v == null; // Equivalent: v === null || v === undefined; The JavaScript language spec explicitly defines null as a value ...
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;

🌐
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 ...
Find elsewhere
🌐
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.
🌐
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. This operator only passes for null values, not for undefined, false, 0, NaN. ... Example: The following code snippets show some comparison of objects.
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript - Dmitri Pavlutin
September 21, 2020 - Turns out typoef null being 'object' was a mistake in the early JavaScript implementation. Do not use typeof operator to detect a null value. As mentioned previously, use the strict equality operator myVar === null. If you'd like to check whether a variable contains an object using typeof operator, you have to check againts null too:
🌐
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.
🌐
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
November 8, 2023 - Using the || (or) operator, the message text will read “Unknown days since last post” if the daysSinceLastPost value doesn’t exist. const message = `${daysSinceLastPost || 'Unknown'} days since last post` The issue here is that if daysSinceLastPost is 0, the text will read “Unknown days since last post” too! Instead, we could use the ?? operator, otherwise known as the nullish coalescing operator.
🌐
W3Schools
w3schools.com › js › js_mistakes.asp
JavaScript Common Mistakes
To solve this problem, you must test if an object is not null, and not undefined. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
The loose equality operator coerces operands of different types to boolean values, making null and undefined both false. The strict equality operator considers operands of different data types to be unequal. null == undefined > true null === undefined > false · Unlike the reserved keyword null, undefined is a property of the global object. This was a design decision made early in JavaScript's development, and it let legacy browsers overwrite undefined completely.
🌐
Code.mu
code.mu › en › javascript › manual › lang › null
The null value - denotes the absence of an object or element in JavaScript
The null value means "nothing", i.e. the absence of an object or element. When performing logical operations, it is equivalent to a false statement or false. JavaScript has a similar undefined value that denotes the absence of a value.
Top answer
1 of 12
91

The question isn't really "why is there a null value in JS" - there is a null value of some sort in most languages and it is generally considered very useful.

The question is, "why is there an undefined value in JS". Major places where it is used:

  1. when you declare var x; but don't assign to it, x holds undefined;
  2. when your function gets fewer arguments than it declares;
  3. when you access a non-existent object property.

null would certainly have worked just as well for (1) and (2)*. (3) should really throw an exception straight away, and the fact that it doesn't, instead of returning this weird undefined that will fail later, is a big source of debugging difficulty.

*: you could also argue that (2) should throw an exception, but then you'd have to provide a better, more explicit mechanism for default/variable arguments.

However JavaScript didn't originally have exceptions, or any way to ask an object if it had a member under a certain name - the only way was (and sometimes still is) to access the member and see what you get. Given that null already had a purpose and you might well want to set a member to it, a different out-of-band value was required. So we have undefined, it's problematic as you point out, and it's another great JavaScript 'feature' we'll never be able to get rid of.

I actually use undefined when I want to unset the values of properties no longer in use but which I don't want to delete. Should I use null instead?

Yes. Keep undefined as a special value for signaling when other languages might throw an exception instead.

null is generally better, except on some IE DOM interfaces where setting something to null can give you an error. Often in this case setting to the empty string tends to work.

2 of 12
39

Best described here, but in summary:

undefined is the lack of a type and value, and null is the lack of a value.

Furthermore, if you're doing simple '==' comparisons, you're right, they come out the same. But try ===, which compares both type and value, and you'll notice the difference.