Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/shared/md/2019-03-19-indexes-and-length-of-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
**Never add items to array usin indexes. Never trust them!**

Lets create an empty array, and check its length is `0`
```
var arr = [];
arr.length; // 0
```

Lets add something to its first place:

```
var arr = []
arr[0] = 'I am the first';
arr.length; // 1
```

Oh, what if...


```
var arr = []
arr[3] = 'I am the first';
arr.length; // 4
```

```
var arr = []
arr[0] = 'I am the first';
arr[15] = 'actually, i am the second';
arr.length; // 16
```

***WTF?***

Array actually looks like
```
["I am the first", empty × 14, "actually, i am the second"]
```

** Explanation **

This is how array's `.length` works. It takes index of the last elemnt, and returns `index+1`.

Use native array methods to add items to it: `push`, `unshift`.

— [@dogusev][1]

[1]:https://github.com/dogusev