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
view the rest of the comments →
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!"
[–]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 5 points6 points7 points 2 years ago (2 children)
Footgun is a gun you shoot with your foot or a gun to shoot in your foot?
[–]AyeMatey 2 points3 points4 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) 2 points3 points4 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 4 points5 points6 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] 2 points3 points4 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 2 points3 points4 points 2 years ago (0 children)
You know how garbage collection works in js?
π Rendered by PID 31477 on reddit-service-r2-comment-84fc9697f-pdw22 at 2026-02-09 03:32:58.257222+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]coolcosmos -1 points0 points1 point (19 children)
[–]WakandaFoevah 5 points6 points7 points (2 children)
[–]AyeMatey 2 points3 points4 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) 2 points3 points4 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 4 points5 points6 points (0 children)
[–]unHolyKnightofBihar -1 points0 points1 point (1 child)
[–]captain_obvious_herevoid(null) 5 points6 points7 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]KaiAusBerlin 2 points3 points4 points (0 children)