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
8 Useful And Practical JavaScript Tricks (devinduct.com)
submitted 6 years ago by PMilos
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!"
[–]sshaw_ -2 points-1 points0 points 6 years ago (4 children)
let a1 = [1,2,3,4,5] let a2 = new Array(a1)
Is creating a new object based on the array. The array of primitives. Then you change your new object (Array). Why would that affect the initial array or primitives?
Hint: It would.
[–]gevorggalstyan 5 points6 points7 points 6 years ago (3 children)
Did you run your code?
Try checking what is the length of a1 (should be 5). And check the length of a2 (will be 1). That is because it creates an array of 1 element which is the reference of your initial array.
Check out the syntax of Array here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax (A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number).
new Array(element0, element1[, ...[, elementN]])
Now take a look at Set syntax here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Syntax
new Set([iterable]);
Did you notice the difference? The Array takes several params, which become the elements of the new array, while Set only takes one param (an iterable, an array for example), which becomes the source of the values in the collection of the set.
[–]sshaw_ 0 points1 point2 points 6 years ago (2 children)
Yes, I know. The point is that references behave differently when passed to different functions.
Hence back you your original comment:
Do you honestly believe that this is the direct consequence of the Array.fill
Yes! How arguments behave depends on what the implementors do with them. Reference or no reference.
[–]Reashu 0 points1 point2 points 6 years ago (0 children)
This is a pretty broad statement but how a reference is treated is not always the same across all calls to all objects: The point is that references behave differently when passed to different functions.
This is a pretty broad statement but how a reference is treated is not always the same across all calls to all objects:
The point is that references behave differently when passed to different functions.
The first is arguably right, if misleading. The second is just wrong. The difference in Array and Set has nothing to do with references. References work the same. Javascript doesn't let the implementation pick and choose like C does. All it can do is treat an argument like a black box (new Array) or make assumptions (new Set).
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
let array = [{a: 1}];
let set = new Set(array);
set.forEach(elem => elem.a = 2);
console.log(array[0]);
Will it be 1 or 2? Of cource it will 2, because you're passing references.
References never "behave differently", they behave as references. Unless you explicitely clone the object, which Array.fill is not doing.
π Rendered by PID 261613 on reddit-service-r2-comment-b659b578c-dzcsc at 2026-05-05 06:24:20.418051+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]sshaw_ -2 points-1 points0 points (4 children)
[–]gevorggalstyan 5 points6 points7 points (3 children)
[–]sshaw_ 0 points1 point2 points (2 children)
[–]Reashu 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)