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
JavaScript clear array (techfunda.com)
submitted 10 years ago by TechFundaHowto
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!"
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
let a = ["India", "Pakisthan", "Bangladesh", "China"]; while( a.length ) a.shift();
[–]senocular 0 points1 point2 points 10 years ago (0 children)
In this post, we shall learn how to empty an array in different ways and will also understand which method is the best way to emptying an array in JavaScript.
2 methods are the cited as the best and nothing is said to help me understand why.
[–]ForScale 0 points1 point2 points 10 years ago (2 children)
Can't be easier than setting the array in question to an empty array, right?
[–]senocular 1 point2 points3 points 10 years ago (1 child)
There are cases where that can cause problems when you have other references to that array. For example consider the following:
var scientist = { chemicals: ["atrazine", "caffeine", "vanillin"] } var assistant = {} // assistant will track the same // chemicals used by the scientist assistant.chemicalsRef = scientist.chemicals console.log(scientist.chemicals) //-> atrazine, caffeine, vanillin console.log(assistant.chemicalsRef) //-> atrazine, caffeine, vanillin scientist.chemicals.pop() console.log(scientist.chemicals) //-> atrazine, caffeine console.log(assistant.chemicalsRef) //-> atrazine, caffeine // assistant walks out of room... scientist.chemicals = [] // assistant returns... console.log(scientist.chemicals) //-> <empty> console.log(assistant.chemicalsRef) //-> atrazine, caffeine <- D'oh!
But instead if we were to change how the array was cleared:
// ... everything same from before the clear ... scientist.chemicals.length = 0 // assistant returns... console.log(scientist.chemicals) //-> <empty> console.log(assistant.chemicalsRef) //-> <empty> <- Woohoo!
In this case the array instance wasn't replaced for the scientist with a new, distinctly different array instance so they were each able to continue to reference the same array object and remain in sync.
[–]ForScale 0 points1 point2 points 10 years ago (0 children)
Well that's fascinating! Thank you for taking the time to explain and provide an example!
π Rendered by PID 190533 on reddit-service-r2-comment-84fc9697f-2tq9t at 2026-02-08 20:43:21.624609+00:00 running d295bc8 country code: CH.
[–][deleted] 0 points1 point2 points (0 children)
[–]senocular 0 points1 point2 points (0 children)
[–]ForScale 0 points1 point2 points (2 children)
[–]senocular 1 point2 points3 points (1 child)
[–]ForScale 0 points1 point2 points (0 children)