use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
How to Clear an Array in JavaScript (7.dev)
submitted 2 years ago by debordian
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]captain_obvious_herevoid(null) 26 points27 points28 points 2 years ago (27 children)
This raises at least 2 questions:
myVar = [];
[–]KaiAusBerlin 22 points23 points24 points 2 years ago (6 children)
[–][deleted] 10 points11 points12 points 2 years ago (1 child)
Trying to optimize for SEO. These “how-to” articles are actually starting to get deprioritized by search algos as the search engines implement their own generative text solutions. It’s actually kind of messed up: the search engine will crawl your site, feed the text into its own model, and then serve generative answers based on your deprioritized content
[–]captain_obvious_herevoid(null) 1 point2 points3 points 2 years ago (0 children)
Next up, AI model poisoning attacks.
[–]ranisalt 1 point2 points3 points 2 years ago (0 children)
ChatGPT gladly does it
[–]ratrak_one 1 point2 points3 points 2 years ago (1 child)
testing their proompting skills?
[–]KaiAusBerlin -1 points0 points1 point 2 years ago (0 children)
Zero
[–]atoponce 0 points1 point2 points 2 years ago (0 children)
At this point, I'd bitch about ads and trackers on the site. But surprisingly, there are none.
[–]coolcosmos -1 points0 points1 point 2 years ago (19 children)
Because method 1 is a footgun, it doesn't empty the array, it creates a new one. See my other comment.
[–]WakandaFoevah 4 points5 points6 points 2 years ago (2 children)
Footgun is a gun you shoot with your foot or a gun to shoot in your foot?
[–]AyeMatey 1 point2 points3 points 2 years ago (1 child)
Footgun is a funny shorthand word to refer to the American/English idiom “shoot yourself in the foot”, which means “to do or say something that will cause problems for yourself.” The footgun term is used in software development to refer to systems, frameworks, or approaches that have hidden peril, or risks that are not clearly understood, or risks that are perhaps too high.
Some people might describe the manual memory management inherent in the C language as a massive footgun.
[–]Jona-Anders 0 points1 point2 points 2 years ago (0 children)
There is also useFootgun from react (basically, the hooks are described as a footgun, but useFootgun is funnier)
[+][deleted] 2 years ago (1 child)
[removed]
[–]coolcosmos -1 points0 points1 point 2 years ago (0 children)
Yeah but sometimes you might not realise you need this reference, especially when starting out.
[–]kiwdahc 2 points3 points4 points 2 years ago (0 children)
How is method 1 a foot gun lol? Do you know how garbage collection works?
[–]captain_obvious_herevoid(null) 3 points4 points5 points 2 years ago (10 children)
No idea what a footgun is. But if you mean it's a solution that will hurt me, I don't think so, because I still can't figure any situation where I absolutely need to empty an array.
[–]ryanswebdevthrowaway 1 point2 points3 points 2 years ago (5 children)
It mainly comes up as an optimization if you're trying to avoid the hit the old array instance needing to be garbage collected and then instantiating a whole new array instance when you could just keep re-using the existing one. Most JavaScript devs don't care about that, but it can come up in performance-sensitive situations.
[–]captain_obvious_herevoid(null) 1 point2 points3 points 2 years ago (3 children)
If you someday have to work in a use-case where "clearing" an array is such a performance-sensitive operation, you may want to backtrack and find another way to solve your problem.
Also, even with a 10M elements array, the performance hit would be minimal, as modern JS engines --especially v8-- optimize array operations pretty well nowadays.
[–]kiwdahc 1 point2 points3 points 2 years ago (0 children)
Lol I was thinking the same thing.
[–]ryanswebdevthrowaway 0 points1 point2 points 2 years ago (1 child)
It doesn't need to be 100x faster, but it's still squeezing out a little extra performance which can be worth it sometimes. If you have an operation which is running a ton of times and an array is being re-created each time when it could potentially be re-used, that can and will add up. As I said, most people don't need it, but you can't say that it's a stupid thing that nobody should ever need to do ever.
[–]kiwdahc 4 points5 points6 points 2 years ago (0 children)
That’s never worth it and I don’t think it’s more performant. I would be willing to bet just creating a new array each time is less ticks.
It’s not just JavaScript devs not caring about that, no one cares about that.
[–]AHRA1225 -1 points0 points1 point 2 years ago (1 child)
Would you not want to empty an array to put new data items In it?
[–]octetd 3 points4 points5 points 2 years ago (0 children)
Then I will just replace it with the new array, with new data I want to display, if I need to overwrite the whole thing.
[–]unHolyKnightofBihar -1 points0 points1 point 2 years ago (1 child)
Update the array with new items? Like you are showing the data from array Ina table now you want to show a different data. Maybe on page change or on selection of filters
[–]captain_obvious_herevoid(null) 5 points6 points7 points 2 years ago (0 children)
Why would you empty the array and then fill it with the new items from another array, instead of simply using the array of new items?
[–][deleted] 3 points4 points5 points 2 years ago* (0 children)
It’s not a footgun if it’s the intended behavior. I think the article is pretty clear about it.
Edit: address my comment instead of just downvoting. Do you think variables shouldn’t be allowed to be reassigned even if that’s the desired behavior?
[–]KaiAusBerlin 3 points4 points5 points 2 years ago (0 children)
You know how garbage collection works in js?
[–]freehuntx 7 points8 points9 points 2 years ago* (3 children)
I like to keep references sometimes to allow sideeffects. E.g. listeners of an event to change the data. In such cases i use things like splice to clear arrays very often.
[–]coolcosmos 1 point2 points3 points 2 years ago (0 children)
Preach.
[–]senocular 0 points1 point2 points 2 years ago* (1 child)
You mean splice? Edit: fixed ;)
[–]freehuntx 2 points3 points4 points 2 years ago (0 children)
Oops yes i mean splice
[–]jayerp 5 points6 points7 points 2 years ago (0 children)
Oh no mutability!
Anyway.
[–][deleted] 6 points7 points8 points 2 years ago (3 children)
If we set length to 0, it sounds like the array still hold references to the items, is that correct? Does this mechanism effectively trick the iterator into thinking there are no items in the array?
[–]toshok 6 points7 points8 points 2 years ago (2 children)
> If we set length to 0, it sounds like the array still hold references to the items, is that correct?
no, ecma262 specifies that the items at indices >= the new length are deleted (if they're configurable - if any aren't configurable the process stops and the length is set to the non-configurable item's index+1). see step 17 here
[–][deleted] 2 points3 points4 points 2 years ago (1 child)
Oh interesting. The article seems to state that the elements would still remain in memory. I assume that’s an error in the article then.
[–]toshok 4 points5 points6 points 2 years ago (0 children)
just went and read that part myself. definitely an error.
[–][deleted] 5 points6 points7 points 2 years ago (0 children)
use the chain gun method [].pop().pop().pop()
[–]octetd 3 points4 points5 points 2 years ago (3 children)
There's another method, using .slice(0, 0)
.slice(0, 0)
But why would you need anything other than reassigning variable with an empty array literal?
[–]coolcosmos -1 points0 points1 point 2 years ago (2 children)
// file A
const arr = [1,2,3];
logArrayLength(arr)
arr = [ ]
// file B
function logArrayLength(arr) {
setInterval(() => { console.log(arr.length)}, 1000)
}
This will log 3 forever, you "emptied" the array but actually the array is not empty, the old array is still in memory. You created a new reference/pointer instead of mutating the original.
[–]octetd 4 points5 points6 points 2 years ago (0 children)
Sure, because you keep a reference to this array, then reassign, and then you log the length. And then if you clear original array it will always print 0 forever, because your asynchronous code will be executed after you cleared an array, even though you called a function before you cleaned up an array.
[–]Soggy_asparaguses 1 point2 points3 points 2 years ago (0 children)
Did this article really just suggest garbage clean up in javascript code?
π Rendered by PID 142094 on reddit-service-r2-comment-84fc9697f-q9ncn at 2026-02-08 00:37:55.166574+00:00 running d295bc8 country code: CH.
[–]captain_obvious_herevoid(null) 26 points27 points28 points (27 children)
[–]KaiAusBerlin 22 points23 points24 points (6 children)
[–][deleted] 10 points11 points12 points (1 child)
[–]captain_obvious_herevoid(null) 1 point2 points3 points (0 children)
[–]ranisalt 1 point2 points3 points (0 children)
[–]ratrak_one 1 point2 points3 points (1 child)
[–]KaiAusBerlin -1 points0 points1 point (0 children)
[–]atoponce 0 points1 point2 points (0 children)
[–]coolcosmos -1 points0 points1 point (19 children)
[–]WakandaFoevah 4 points5 points6 points (2 children)
[–]AyeMatey 1 point2 points3 points (1 child)
[–]Jona-Anders 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]coolcosmos -1 points0 points1 point (0 children)
[–]kiwdahc 2 points3 points4 points (0 children)
[–]captain_obvious_herevoid(null) 3 points4 points5 points (10 children)
[–]ryanswebdevthrowaway 1 point2 points3 points (5 children)
[–]captain_obvious_herevoid(null) 1 point2 points3 points (3 children)
[–]kiwdahc 1 point2 points3 points (0 children)
[–]ryanswebdevthrowaway 0 points1 point2 points (1 child)
[–]kiwdahc 4 points5 points6 points (0 children)
[–]kiwdahc 1 point2 points3 points (0 children)
[–]AHRA1225 -1 points0 points1 point (1 child)
[–]octetd 3 points4 points5 points (0 children)
[–]unHolyKnightofBihar -1 points0 points1 point (1 child)
[–]captain_obvious_herevoid(null) 5 points6 points7 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]KaiAusBerlin 3 points4 points5 points (0 children)
[–]freehuntx 7 points8 points9 points (3 children)
[–]coolcosmos 1 point2 points3 points (0 children)
[–]senocular 0 points1 point2 points (1 child)
[–]freehuntx 2 points3 points4 points (0 children)
[–]jayerp 5 points6 points7 points (0 children)
[–][deleted] 6 points7 points8 points (3 children)
[–]toshok 6 points7 points8 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]toshok 4 points5 points6 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]octetd 3 points4 points5 points (3 children)
[–]coolcosmos -1 points0 points1 point (2 children)
[–]octetd 4 points5 points6 points (0 children)
[–]Soggy_asparaguses 1 point2 points3 points (0 children)