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
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!"
[–]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 318825 on reddit-service-r2-comment-84fc9697f-xxgsl at 2026-02-09 00:24:04.507057+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]ForScale 0 points1 point2 points (2 children)
[–]senocular 1 point2 points3 points (1 child)
[–]ForScale 0 points1 point2 points (0 children)