Skip to content

Commit

Permalink
new file: javascript/filling-up-array.md
Browse files Browse the repository at this point in the history
  • Loading branch information
byt3h3ad committed Oct 18, 2023
1 parent 6d65013 commit 1aaf52d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions javascript/filling-up-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# How to Not Fill Up an `Array`

What gets logged?

```js
const array = new Array(3).fill([]);
array[0].push("bytes");
console.log(array); // [ ["bytes"], ["bytes"], ["bytes"] ]
```

The key to understanding this one is in knowing that arrays in JavaScript are [reference values](https://ui.dev/primitive-vs-reference-values-in-javascript).

When you call `.fill([])`, what you’re really doing is “filling up” the array with three _references_ to the same array. You can kind of think of it like this.

```js
const reference = [];
const array = new Array(3).fill(reference);
```

Where now, `array` has three elements and they’re all referencing the same `reference` array. Therefore, if you add an item to any of the three elements, since they all point to the same array, it’s _as if_ you’re adding an item to all of them.

To get the same functionality without the referential weirdness, you can use `Array.from`.

```js
const array = Array.from({ length: 3 }, () => []);
array[0].push("bytes"); // [ ["bytes"], [], [] ]
```

[source](https://bytes.dev/archives/231)
2 changes: 2 additions & 0 deletions javascript/sort-alphabetically.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ function sortAlphabetically(arr) {
return arr.sort((a, b) => collator.compare(a.firstName, b.firstName));
}
```

[source](https://bytes.dev/archives/214)

0 comments on commit 1aaf52d

Please sign in to comment.