With EcmaScript 6 (ES2105), creating an array containing five nulls is as easy as this:

const arr = new Array(5).fill(null);

MDN Reference

Answer from lardois on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › is-there-any-way-to-check-if-there-is-a-null-value-in-an-object-or-array-in-javascript
Is there any way to check if there is a null value in an object or array in JavaScript?
This is due to the fact that a declared variable that has not yet been given a value is undefined rather than null. There are many ways to check whether a value is null or not in JavaScript. Let's discuss few ways one by one. The Object.keys() is a method in JavaScript that returns an array of the keys of an object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
JavaScript is unique to have two nullish values: null and undefined. 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 ...
🌐
SheCodes
shecodes.io › athena › 6421-how-to-check-array-for-null-values-in-javascript
[JavaScript] - How to Check Array for Null Values in | SheCodes
Learn how to use the Array.prototype.filter() method to check if an array has any null values, and use filters to exclude them from the output.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-null-values-from-array
Remove Null or Undefined Values from an Array in Javascript | bobbyhadz
Use the forEach() method to iterate over the array. Check if each element is not equal to null. Push the elements that meet the condition into the results array.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › null
`null` in JavaScript - Mastering JS
function isObject(v) { return typeof v === 'object' && v !== null; } Did you find this tutorial useful? Say thanks by starring our repo on GitHub! The `setTimeout()` Function in JavaScript · JavaScript Array flatMap() How to Get Distinct Values in a JavaScript Array ·
Find elsewhere
🌐
Syncfusion
syncfusion.com › blogs › post › null-vs-undefined-in-javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - //Out of bound array elements are undefined. const array1 = [0,1,2,3]; console.log("The 4th element in the array is ",array1[4]); /* Result: The 4th element in the array is undefined. */ Everything a developer needs to know to use JavaScript control in the web app is completely documented. ... Even though there are fundamental differences between null and undefined (which will be discussed in the next section in detail), programmers often tend to use them interchangeably due to the following similarities:
🌐
Medium
medium.com › javascript-scene › handling-null-and-undefined-in-javascript-1500c65d51ae
Handling null and undefined in JavaScript | by Eric Elliott | JavaScript Scene | Medium
November 12, 2019 - But JavaScript already has a data type that implements a lot of those features out-of-the-box, so I usually reach for that instead: The Array. If you want to create a function which may or may not produce a result (particularly if there can be more than one result), you may have a great use-case to return an array. const log = x => console.log(x); const exists = x => x != null;const arr = [1,2,3]; const find = (p, list) => [list.find(p)].filter(exists); find(x => x > 3, arr).map(log); // does not log anything find(x => x < 3, arr).map(log); // logs 1
🌐
Designcise
designcise.com › web › tutorial › how-to-remove-null-values-from-a-javascript-array
How to Remove null Values From a JavaScript Array? - Designcise
November 6, 2022 - To remove only null values from a JavaScript array, you can do the following: Use Array.prototype.filter(); Use a Loop. Using Array.prototype.filter() When the provided callback function to Array.prototype.filter() returns true, the value from ...
🌐
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.
🌐
Medium
medium.com › @gaelgthomas › remove-null-values-from-array-in-javascript-988298b01e3b
Remove Null Values From Array in JavaScript | by Gaël Thomas | Medium
September 24, 2022 - You’ll notice the syntax will differ a little, but the function we’ll use is the same as the one built-in in JavaScript. Indeed, the Lodash function is called filter. It’s time for an example! import _ from "lodash"const whatIsLife = ["Life", "is", null, "beautiful", null, "!"]const thisIsLife = _.filter(whatIsLife, (element) => element !== null)console.log(thisIsLife) // Output: ["Life", "is", "beautiful", "!"]
🌐
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 - Even if you were to do things in ... an entry in an array that is out of bounds, C# would throw, whereas JavaScript would simply return undefined. ... We’ve got an empty array, and then we try to print out the fifth element from the array. This is out of bounds. The result of this code is undefined. Basically, there are three conditions that we want to account for when checking for values that we think could be null or ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - The typeof null returns 'object', which is historical bug in JavaScript that may never be fixed. Use the optional chaining operator (?.) to access the property of an object that may be null. Was this tutorial helpful ? Yes No · Send Cancel · Previously · How to Check if an Array Contains a Value in Javascript ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › null-in-javascript
Null in JavaScript - GeeksforGeeks
June 5, 2024 - In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › check-if-an-array-is-empty-or-not-in-javascript
Check if an array is empty or not in JavaScript - GeeksforGeeks
July 11, 2025 - let a = []; if (Array.isArray(a) && a.length === 0) { console.log("Empty"); } else { console.log("Not Empty"); } ... Help us improve. Share your suggestions to enhance the article.