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?

Answer from chiliNUT on Stack Overflow
Top answer
1 of 12
183

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

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript - MDN Web Docs
October 28, 2025 - 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

NULL vs undefined as explicit return typ
Nobody seems to agree on this. In my opinion having two values for null is dumb. I’d like to only have one value that represents a null value. Since it’s not possible to avoid using undefined, I avoid using null instead. There’s an eslint rule that you can use that will give an error every time null is used. That’s the best solution IMO More on reddit.com
🌐 r/typescript
11
8
July 13, 2023
A question about DOM, why is getElementById returning a null or undefined here?
You want to set onload to a function reference. You're calling test() rather than assigning test which sets onload to the return value of test(). Instead use: document.onload = test; More on reddit.com
🌐 r/learnjavascript
10
3
November 8, 2017
Promise.resolve() vs return inside async function
I'm not familiar with TS, but i try to answer. Promise.resolve() will create new promise which is already resolved with value you passed to it. And you immediately discard this new promise (you do not return it or save to variable and use somewhere else). TS error tells you what you need to return something from await function, so this is why return is required there. More on reddit.com
🌐 r/learnjavascript
6
1
January 18, 2020
Can TypeScript not infer when an array of (T | undefined)[] is converted to T[] by a .filter call?

typeof operator is preferable here else zeroes will get filtered out as falsey.

More on reddit.com
🌐 r/typescript
41
22
November 27, 2018
🌐
Syncfusion
syncfusion.com › blogs › javascript › null vs. undefined in javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - JavaScript always assigns undefined to indicate the absence of a value by default, and it never automatically assigns null as a value. A variable will only return null if a programmer or an API specifically assigns the value null to the variable.
🌐
Medium
vvkchandra.medium.com › essential-javascript-mastering-null-vs-undefined-66f62c65d16b
Essential JavaScript: Mastering null vs undefined | by Chandra Gundamaraju | Medium
September 9, 2020 - JavaScript returns undefined by default when a function doesn’t return any other value. If a function returns null explicitly, then null is returned, not undefined.
🌐
Laracasts
laracasts.com › discuss › channels › javascript › how-to-return-null-instead-of-undefined-using-find
How to return null instead of undefined using .find()
As far a I know, you can't do anything inside the callback to force a result other than undefined whenever nothing is found, so you would need to use Short Circuit Evaluation to get something other than undefined ... @godzilaravel be aware that this approach will work for the given example - but if your find test was different and an item is found within the array which is falsey (null, false, 0, ...) then it produce null as the result everytime.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › null
null - JavaScript | MDN
May 23, 2022 - 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
Find elsewhere
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2021-01 › null-vs-undefined
Null Vs Undefined - Web Dev Simplified Blog
January 11, 2021 - If no entry exists it makes the most sense to return null since you are stating that there is no value found. On the other hand undefined means that there is no value because no value has been set yet.
🌐
Medium
medium.com › @o_o_o › null-vs-undefined-can-i-use-only-one-a3b7db5468f2
null vs. undefined: Can I use only one? | by OOO | Medium
March 29, 2022 - They simply don’t render.”, returning undefined from a component throws following error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null. It clearly states that if want ...
🌐
xjavascript
xjavascript.com › blog › is-it-better-to-return-undefined-or-null-from-a-javascript-function
JavaScript Functions: undefined vs null – Which Should You Return?
null is also a primitive value, but unlike undefined, it is explicitly assigned to indicate an intentional absence of value. It is a programmer-defined signal that “no value exists here.” · Explicit assignment: You must intentionally set a variable or return value to null. let user = null; // Explicitly indicating "no user yet" Historical note: Despite being a primitive, typeof null returns "object" (a long-standing JavaScript bug).
🌐
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
January 21, 2026 - If the goal is to get the highest quality of code with the minimal amount of work, this is not a good candidate for meeting that goal. In practice, it is easy to capture the null values any time that they would get introduced into your code and immediately switch that value to undefined. e.g. for function return values you can do something like:
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-determine-if-variable-is-undefined-or-null-in-javascript.php
How to Determine If Variable is Undefined or NULL in JavaScript
<script> var firstName; var lastName = null; // Try to get non existing DOM element var comment = document.getElementById('comment'); console.log(firstName); // Print: undefined console.log(lastName); // Print: null console.log(comment); // Print: null console.log(typeof firstName); // Print: undefined console.log(typeof lastName); // Print: object console.log(typeof comment); // Print: object console.log(null == undefined) // Print: true console.log(null === undefined) // Print: false /* Since null == undefined is true, the following statements will catch both null and undefined */ if(firstNa
🌐
Coddy.Tech
coddy.tech › docs › null vs undefined
Runnable JavaScript Docs: null vs undefined | Coddy
April 23, 2026 - typeof null returning "object" is a bug from 1995 that couldn't be fixed without breaking existing websites, so it's been preserved forever. null is not an object - it's a primitive, same as undefined, numbers, strings, and booleans. The typeof result just lies about it. Practical consequence: typeof is useless for detecting null. Use direct comparison: const value = null; if (value === null) { console.log("it's null"); } if (typeof value === "undefined") { console.log("it's undefined"); }
🌐
Exercism
exercism.org › tracks › javascript › concepts › null-undefined
Null and Undefined in JavaScript on Exercism
You can check whether a variable is null by using the strict equality operator ===. Although null is a primitive value, the typeof operator "wrongly" returns object for historic reasons. That means it cannot be used by itself to check whether a variable is null. let name = null; name === null; // => true // Pitfall: typeof name; // => 'object' A variable that has not been assigned a value is of type undefined.1 · That means while null represents an empty value (but still a value), undefined represents the total absence of a value. 🤯 ... If a variable is declared without a value (initialization), it is undefined.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript - MDN Web Docs - Mozilla
A variable that has not been assigned a value is of type undefined. A function returns undefined if a value was not returned. Accessing a property that does not exist also returns undefined.
🌐
Modern Web
modernweb.com › frontend development › exploring the abyss of null and undefined in javascript
Exploring The Abyss Of Null And Undefined In JavaScript | Modern Web - Web3, Business & Technology
October 30, 2022 - Because it is an external API, attempting to retrieve a non-existent element returns a null value rather than undefined. Generally, if you need to assign a non-value to a variable or property, pass it to a function, or return it from a function, null is almost always the best option. To put it simply, JavaScript uses undefined and programmers should use null.
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-null-and-undefined
Difference between null and undefined in JavaScript
A variable has undefined when no value assigned to it. ... The '' is not the same as null or undefined.
🌐
CodeForGeek
codeforgeek.com › javascript-null-undefined
Null vs undefined in JavaScript: What the difference actually means | CodeForGeek
1 week ago - The describe() function returns separate labels for null and undefined, then leaves every other value in the present branch. import assert from 'node:assert/strict'; function describe(value) { if (value === null) return 'null'; if (value === undefined) return 'undefined'; return 'present'; } console.log(describe(null)); console.log(describe(undefined)); console.log(null === undefined); console.log(null == undefined); assert.equal(describe(null), 'null'); assert.equal(describe(undefined), 'undefined'); assert.equal(null === undefined, false); assert.equal(null == undefined, true);
🌐
DEV Community
dev.to › rakibrahman › javascript-essentials-handling-null-undefined-and-safely-accessing-data-with-and--5c6b
JavaScript Essentials: Handling Null, Undefined, and Safely Accessing Data with ?? and ?. - DEV Community
February 1, 2025 - Optional chaining (?.) operator is used to access properties and methods calls of an object. If the object value is either null or undefined it will return undefined instead of throwing an error, otherwise it will perform the operation.