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"! Answer from jack_waugh on reddit.com
🌐
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.
🌐
Reddit
reddit.com › r/typescript › undefined vs null
r/typescript on Reddit: Undefined vs null
February 27, 2023 -

Since switching to TypeScript I have been using a lot of optional properties, for example:

type store = {
  currentUserId?: string
}

function logout () {
  store.currentUserId = undefined
}

However my coworkers and I have been discussing whether null is a more appropriate type instead of undefined, like this:

type store = {
  currentUserId: string | null
}

function logout () {
  store.currentUserId = null
}

It seems like the use of undefined in TypeScript differs slightly from in Javascript.

Do you guys/girls use undefined or null more often? And, which of the examples above do you think is better?

🌐
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.

🌐
Reddit
reddit.com › r/javascript › null vs undefined
r/javascript on Reddit: null vs undefined
June 21, 2017 - ... I try to think of it simply as: undefined means exactly that, we have not bothered to set a value for this thing. null means we explicitly set a value and wanted it to be anomalous.
🌐
Reddit
reddit.com › r/learnjavascript › not sure when to use undefined and null in javascript?
r/learnjavascript on Reddit: Not sure when to use Undefined and Null in JavaScript?
September 29, 2014 - When the typeof keyword is used on undefined it outputs "undefined" which is correct. When typeof is used on null it outputs "object "which is not correct and is a flaw in the language that was never fixed.
🌐
Reddit
reddit.com › r/learnjavascript › is my interpretation of null vs. undefined correct?
r/learnjavascript on Reddit: Is my interpretation of null vs. undefined correct?
June 23, 2018 -
  • Undefined: The variable exists, but it has not yet been give a value, and thus it has a default value of undefined.

  • Null: The variable exists only as a reference. It does not yield any value.

Follow-up: Why is it useful to distinguish between null and undefined in javascript?

Top answer
1 of 3
6

This does not completely cover all cases.

Undefined doesn't necessarily mean that a variable exists but hasn't been given a value yet. It's also possible to explicitly give a variable the value of "undefined", and it is sometimes returned when trying to look up an object member that doesn't exist. E.g. when looking up myObj.thisDoesntExist.

Variables that are set to null do return a value: the value being null.

They need to both be taken into account because they behave slightly differently. For example, if you have a function with default arguments, the default argument will be used if you pass undefined (or no argument at all) - but if you pass null, then your null will be used.

2 of 3
2

Not... exactly.

undefined means that either the value of the variable has not yet been defined, OR you've explicitly given the variable the value of undefined.

null is essentially an empty value that's separate from more generic (specific?) empty values, like empty string or zero or empty array. It's the equivalent of saying "this box (variable) that can hold a value currently is empty".

Finally, not defined (not really a value, but worth mentioning) is a special error-case when you try to access a variable before initializing it. For example, if you load an empty page and type in the console console.log(derp), you'll get a message that says "Uncaught ReferenceError: derp is not defined". This final value is a little strange, since it's almost the same as undefined, but won't allow you to access the variable in any format; It doesn't exist yet.

🌐
Reddit
reddit.com › r/learnjavascript › javascript basics undefined vs null
r/learnjavascript on Reddit: JavaScript Basics undefined vs null
September 7, 2018 - I've been using javascript for 8 years, for reference ... Null is a primitive, but it is special, it is an object like more complex structural types, like object itself, arrays, and functions.... The strict equality, or 'identity' operator gives you the real deal, and is almost always what your want, not performing any type conversions: typeof null // "object" typeof undefined // "undefined" null === undefined // false null == undefined // true -- basically just saying both these values are 'falsy' or equal to nothing
Find elsewhere
🌐
Reddit
reddit.com › r/javascript › [askjs] why doesn't js deprecate undefined and replace everything with null?
r/javascript on Reddit: [AskJS] Why doesn't JS deprecate undefined and replace everything with null?
April 16, 2022 -

I never understood why JS makes a distinction between the two. Other programming languages don't have undefined and only have a null value.

I understand that they represent different things, but in practice they are used in exactly the same way. Accessing a property on an undefined/null value gives the same error Cannot read property x of undefined/null. I have never found it useful to make a distinction between the two and I usually have to always make double checks like if (x === undefined || x === null).

Was this just a poor design decision that is here to stay? If someone actually has a use case for knowing the distinction between null vs undefined, I would like to hear it.

🌐
Reddit
reddit.com › r/programmerhumor › what is the difference between null and undefined in javascript?
r/ProgrammerHumor on Reddit: What is the difference between null and undefined in JavaScript?
February 24, 2020 - There is also difference between undefined (the constant) and property which doesn't exist (never set) ... I hate you. I was so happy about the pic and though I understand JS now. Okay, scratch that, I hate JS. ... For anything funny related to programming and software development. ... For anything funny related to programming and software development. ... Chat about javascript and javascript related projects.
🌐
Reddit
reddit.com › r/javascript › basic js question: when to check for undefined, null, etc
r/javascript on Reddit: Basic JS question: when to check for undefined, null, etc
September 11, 2016 -

So I'm usually more of a server side developer, but lately I've been working with more of the client code at work. I understand what undefined and null are in JavaScript, but I find myself always checking for both of them. In fact, when checking if a String property exists, I end up writing this:

if(value !== undefined && value !== null && value !== '')

I figure there is a better way than this, and it's probably because I'm not 100% clear of when to check for what. So if someone could help fill me in here on the rules of when to check for undefined vs null, that would be great.

Top answer
1 of 5
28

TL;DR: Use value != null. It checks for both null and undefined in one step.

In my mind, there are different levels of checking whether something exists:

0) 'property' in object - Returns true if the property exists at all, even if it's undefined or null.

  1. object.property !== undefined - Returns true if the property exists and is not undefined. Null values still pass.

  2. object.property != null - Return true if the property exists and is not undefined or null. Empty strings and 0's still pass.

  3. !!object.property - Returns true if the property exists and is "truthy", so even 0 and empty strings are considered false.

From my experience, level 2 is usually the sweet spot. Oftentimes, things like empty strings or 0 will be valid values, so level 3 is too strict. On the other hand, levels 0 and 1 are usually too loose (you don't want nulls or undefineds in your program). Notice that level 1 uses strict equality (!==), while level 2 uses loose equality (!=).

2 of 5
16

I would just say

if (value) {
  // do stuff
}

because

'' || false
// false
null || false
// false
undefined || false
//false

Edit:

Based on this statement

I end up writing this: if(value !== undefined && value !== null && value !== '')

I initially assumed that what OP was really looking for was a better way to ask "is there a value?", but...

if someone could help fill me in here on the rules of when to check for undefined vs null, that would be great.

If you're looking to see if something is "truthy":

if (foo.bar) {
  alert(foo.bar)
}

This won't alert if value is '', 0, false, null, or undefined

If you want to make sure something is a String so you can use string methods:

if (typeof foo.bar === 'string') {
  alert(foo.bar.charAt(0))
}

This won't alert unless value is of type 'string'.

So.. "when to check for undefined vs null"? I would just say, whenever you know that you specifically need to check for them. If you know that you want to do something different when a value is null vs when a value is undefined, then you can check for the difference. But if you're just looking for "truthy" then you don't need to.

🌐
Reddit
reddit.com › r/learnjavascript › is my interpretation of null, undefined and nan correct?
r/learnjavascript on Reddit: Is my interpretation of null, undefined and NaN correct?
February 29, 2024 -

Suppose I am collecting a sample of movie ratings from 1000 random people to see if they are good. The movies are rated as Number types from -10 to 10.

For example

  1. undefined = no such movie (no value exists)

  2. null = haven't watched the movie (exists, but left undeclared)

  3. 0 = no opinion about the movie (normal value)

  4. NaN = I can't tell if I like it or hate it (indeterminate or invalid number)

Is it correct and is the system good? Tell me in the comments below!

Top answer
1 of 6
10
You're in the ballpark. As a general matter, I would put it like this: undefined - there is nothing because no value has been set null - there is nothing because a value is intentionally absent NaN - an invalid number, it does not exist on the number line, like 0 / 0 or parseInt('banana') Then in your specific use case, I would expect trying to get a rating for a movie that doesn't exist to throw an error or for the movie to be undefined. I would expect any unsubmitted rating to be undefined. It has not been submitted, so it has not been set. I would not expect your app to make use of null or NaN. NaN in particular would be weird to deliberately use in your code. It is essentially an error state. I would also caution about getting too clever with null and undefined. They both mean "nothing" and are not used consistently one way or another at the language level. Having both as a potential value for some property and asking your fellow devs to parse which means what will just lead to confusion.
2 of 6
7
I would disagree with you on undefined, null and NaN. Undefined is when something is declared bit returns no value. const  value; is undefined. Console.log(function (){}); Is undefined and  const obj = {"a": 1}; obj.b; is undefined. Basically you are trying to access a value or reference that does not exist. "I have no reference to the movie you are talking about." Null on the other hand is a deliberate assignment that something does not have a value or reference. Let a = null; "That move does not exist". NaN is any value that is not a number. "How would you rate them movie between -10 and 10?" "Yes."
🌐
Reddit
reddit.com › r/learnjavascript › undefined == null
r/learnjavascript on Reddit: undefined == null
April 29, 2023 -

From what i understand, operands when compared with == undergo coersion and might yield unexpected results.

I was checking 10 == "10", which returned true bcoz string 10 gets converted to numerical 10 here.

I believe it's the same for boolean when compared with numbers.

But would like to know what coersion is taking place when undefined == null takes place. It returns true.

Also, if someone could help me with the difference between them like i'm a five, that would be great.

Anything is appreciated. Thanks in advance.

Top answer
1 of 16
130

I don't really have an answer, but according to Nicholas C. Zakas, page 30 of his book "Professional JavaScript for Web Developers":

When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value null to determine if the variable has been filled with an object reference at a later time

2 of 16
120

At the end of the day, because both null and undefined coerce to the same value (Boolean(undefined) === false && Boolean(null) === false), you can technically use either to get the job done. However, there is right way, IMO.

  1. Leave the usage of undefined to the JavaScript compiler.

    undefined is used to describe variables that do not point to a reference. It is something that the JS compiler will take care for you. At compile time the JS engine will set the value of all hoisted variables to undefined. As the engine steps through the code and values becomes available the engine will assign respective values to respective variables. For those variables for whom it did not find values, the variables would continue to maintain a reference to the primitive undefined.

  2. Only use null if you explicitly want to denote the value of a variable as having "no value".

    As @com2gz states: null is used to define something programmatically empty. undefined is meant to say that the reference is not existing. A null value has a defined reference to "nothing". If you are calling a non-existing property of an object, then you will get undefined. If I would make that property intentionally empty, then it must be null so you know that it's on purpose.

TLDR; Don't use the undefined primitive. It's a value that the JS compiler will automatically set for you when you declare variables without assignment or if you try to access properties of objects for which there is no reference. On the other hand, use null if and only if you intentionally want a variable to have "no value".


Sidebar: I, personally, avoid explicitly setting anything to undefined (and I haven't come across such a pattern in the many codebases/third party libs I've interacted with). Also, I rarely use null. The only times I use null is when I want to denote the value of an argument to a function as having no value, i.e.,:

function printArguments(a,b) {
  console.log(a,b);
}

printArguments(null, " hello") // logs: null hello
🌐
DEV Community
dev.to › sduduzog › null-vs-undefined-what-to-choose-what-to-use-11g
null vs undefined? What to choose? What to use? - DEV Community
August 23, 2023 - "But black dynamite, assigning null to a variable does the same thing" see that's where you'd be wrong. Here's an analogy for you. We can say null is like loading a webpage and just getting a blank screen, but undefined is a '404 not found' error.
🌐
Reddit
reddit.com › r/javascript › why is `typeof null === 'object'` in javascript? the 30-year story of a bug we can't fix
r/javascript on Reddit: Why is `typeof null === 'object'` in JavaScript? The 30-year story of a bug we can't fix
October 14, 2025 - The creator of JavaScript quite literally forgot to have an “if (value == null)” check before checking for the object tag which is 0x000, and since a null pointer is all zeroes, the null value evaluates to true for the object tag. More replies More replies ... Most languages coalesce null and undefined together, and I think that'd make sense here too.
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-null-and-undefined-in-javascript
What is the Difference Between Null and Undefined in JavaScript · CoreUI
February 9, 2025 - According to the typeof operator, however, typeof null returns 'object'—a quirk dating back to the original JavaScript implementation. If you see null console outputs it indicates a deliberate decision to assign null. This is different from a variable that is simply undefined.
Top answer
1 of 11
180

Undefined typically refers to something which has not yet been assigned a value (yet). Null refers to something which definitively has no value. In that case, I would recommend returning a null. Note that a function with no specified return value implicitly returns undefined.

From the ECMAScript2015 spec

4.3.10 undefined value

primitive value used when a variable has not been assigned a value

4.3.12 null value

primitive value that represents the intentional absence of any object value

http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-definitions-undefined-type

Further reading:

When is null or undefined used in JavaScript?

2 of 11
88

I will give you my personal opinionated way of choosing between the two.

My simple question is: could the value, given another input/state/context be defined to something?

If the answer is yes then use null else use undefined. More generally any function returning an object should return null when the intended object does not exist. Because it could exist given another input/state/context.

null represents the absence of value for a given input/state/context. It implicitly means that the concept of the value itself exist in the context of your application but may be absent. In your example the concept of a next card exists but the card itself may not exist. null should be used.

undefined implicitly represents the absence of meaning of that value in your application's context. For example, if I manipulate a user object with a given set of properties and I try to access the property pikatchu. The value of this property should be set to undefined because in my context it doesn't make any sense to have such a property.

🌐
CodeBurst
codeburst.io › javascript-null-vs-undefined-20f955215a2
JavaScript — Null vs. Undefined
January 16, 2018 - But, and this may surprise you, null loosely equals undefined. ... In JavaScript, a double equals tests for loose equality and preforms type coercion.