# Arrays basics As we've seen before, objects in Javascript store arbitrary keyed values. Any string can be a key. But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc. It's difficult to use an object here, because it provides no methods to manage the order of elements. We can't easily access the n-th element. Also we can't insert a new property "before" the existing ones, and so on. It's just not meant for such use. For this purpose, there is a special type of objects in JavaScript, named "an array". [cut] ## Declaration There are two syntaxes for creating an empty array: ```js let arr = new Array(); let arr = []; ``` Almost all the time, the second syntax is used. We can supply the initial elements in the brackets: ```js let fruits = ["Apple", "Orange", "Plum"]; ``` Array elements are numbered, starting with zero. We can get an element by its number in square brackets: ```js run let fruits = ["Apple", "Orange", "Plum"]; alert( fruits[0] ); // Apple alert( fruits[1] ); // Orange alert( fruits[2] ); // Plum ``` We can replace an element: ```js fruits[2] = 'Pear'; // now ["Apple", "Orange", "Pear"] ``` ...Or add to the array: ```js fruits[3] = 'Lemon'; // now ["Apple", "Orange", "Plum", "Lemon"] ``` The total count of the elements in the array is its `length`: ```js run let fruits = ["Apple", "Orange", "Plum"]; alert( fruits.length ); // 3 ``` We can also use `alert` to show the whole array. ```js run let fruits = ["Apple", "Orange", "Plum"]; alert( fruits ); // Apple,Orange,Plum ``` An array can store elements of any type. For instance: ```js run no-beautify // mix of values let arr = [ 'Apple', { name: 'John' }, true, function() { alert('hello'); } ]; // get the object at index 1 and then show its name alert( arr[1].name ); // John // get the function at index 3 and run it arr[3](); // hello ``` ## Methods pop/push, shift/unshift A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is one of most common uses of an array. In computer science, this means an ordered collection of elements which supports two operations: - `push` appends an element to the end. - `shift` get an element from the beginning, advancing the queue, so that the 2nd element becomes the 1st. ![](queue.png) In practice we meet it very often. For example, a queue of messages that need to be shown on-screen. There's another closely related data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)). It supports two operations: - `push` adds an element to the end. - `pop` takes an element to the end. So new elements are added or taken always from the "end". A stack is usually illustrated as a pack of cards: new cards are added to the top or taken from the top: ![](stack.png) Arrays in Javascript can work both as a queue and as a stack. They allow to add/remove elements both to/from the beginning or the end. In computer science such a data structure is called [deque](https://en.wikipedia.org/wiki/Double-ended_queue). **The methods that work with the end of the array:** `pop` : Extracts the last element of the array and returns it: ```js run let fruits = ["Apple", "Orange", "Pear"]; alert( fruits.pop() ); // remove "Pear" and alert it alert( fruits ); // Apple, Orange ``` `push` : Append the element to the end of the array: ```js run let fruits = ["Apple", "Orange"]; fruits.push("Pear"); alert( fruits ); // Apple, Orange, Pear ``` The call `fruits.push(...)` is equal to `fruits[fruits.length] = ...`. **The methods that work with the beginning of the array:** `shift` : Extracts the first element of the array and returns it: ```js let fruits = ["Apple", "Orange", "Pear"]; alert( fruits.shift() ); // remove Apple and alert it alert( fruits ); // Orange, Pear ``` `unshift` : Add the element to the beginning of the array: ```js let fruits = ["Orange", "Pear"]; fruits.unshift('Apple'); alert( fruits ); // Apple, Orange, Pear ``` Methods `push` and `unshift` can add multiple elements at once: ```js run let fruits = ["Apple"]; fruits.push("Orange", "Peach"); fruits.unshift("Pineapple", "Lemon"); // ["Pineapple", "Lemon", "Apple", "Orange", "Peach"] alert( fruits ); ``` ## Internals An array is a special kind of object. Numbers are used as keys. And there are special methods and optimizations to work with ordered collections of data, but at the core it's still an object. Remember, there are only 7 basic types in JavaScript. Array is an object and thus behaves like an object. For instance, it is passed by reference: ```js run let fruits = ["Banana"] let arr = fruits; alert( arr === fruits ); // the same object arr.push("Pear"); // modify it? alert( fruits ); // Banana, Pear alert( arr === fruits ); // still the same single object ``` But what really makes arrays special is their internal representation. The engine tries to store it's elements in the contiguous memory area, one after another, just as painted on the illustrations in this chapter. There are other optimizations as well. But things break if we quit working with an array as with an "ordered collection" and start working with it as if it were a regular object. For instance, technically we can go like that: ```js let fruits = []; // make an array fruits[99999] = 5; // assign a property with the index far greater than its length fruits.age = 25; // create a property with an arbitrary name ``` That's possible, because are objects at base. We can add any properties to them. But the engine will see that we're working with the array as with a regular object. Array-specific optimizations will be turned off, their benefits disappear. The ways to misuse an array: - Add a non-numeric property like `arr.test = 5`. - Make holes, like add `arr[0]` and then `arr[1000]`. - Fill the array in reverse order, like `arr[1000]`, `arr[999]` and so on. Please think of arrays as about special structures to work with the *ordered data*. They provide special methods for that. And there's the `length` property for that too, which auto-increases and decreases when we add/remove the data. Arrays are specially suited and carefully tuned inside Javascript engines to work with ordered data, please use them this way. And if you need arbitrary keys, chances are high that you actually require a regular object `{}`. ## Performance Methods `push/pop` run fast, while `shift/unshift` are slow. ![](array-speed.png) Why is it faster to work with the end of an array than with its beginning? Let's see what happens during the execution: ```js fruits.shift(); // take 1 element from the start ``` It's not enough to take and remove the element with the number `0`. Other elements need to be renumbered as well. The `shift` operation must do 3 things: 1. Remove the element with the index `0`. 2. Move all elements to the left, renumber them from the index `1` to `0`, from `2` to `1` and so on. 3. Update the `length` property. ![](array-shift.png) **The more elements in the array, the more time to move them, more in-memory operations.** The similar thing happens with `unshift`: to add an element to the beginning of the array, we need first to move existing elements to the right, increasing their indexes. And what's with `push/pop`? They do not need to move anything. To extract an element from the end, the `pop` method cleans the index and shortens `length`. The actions for the `pop` operation: ```js fruits.pop(); // take 1 element from the end ``` ![](array-pop.png) **The `pop` method does not need to move anything, because other elements keep their indexes. That's why it's blazingly fast.** The similar thing with the `push` method. ## The for..of and other loops To process all elements, we can use the regular `for` loop: ```js run let arr = ["Apple", "Orange", "Pear"]; *!* for (let i = 0; i < arr.length; i++) { alert( arr[i] ); } */!* ``` That's the most optimized and fastest way to loop over the array. There's an alternative kind of loops: `for..of`. The syntax: ```js for(let item of arr) { // item is an element of arr } ``` Reminds of `for..in`, right? But a totally different beast. The `for..of` loop works with *iterable* objects. An *iterable* is an object that has a special method named `object[Symbol.iterator]`. We don't need to go any deeper now, because this topic deserves a special chapter and it's going to get it. For now it's enough to know that arrays and many other data structures in modern browsers are iterable. That is, they have proper built-in methods to work with `for..of`. So we can loop over an array like this: ```js run let arr = ["Apple", "Orange", "Pear"]; *!* for (let fruit of arr) { */!* alert( fruit ); // Apple, Orange, Pear } ``` Looks clean and nice, right? ````warn header="Don't use `for..in` for arrays" Technically, because arrays are objects, we could use `for..in`: ```js run let arr = ["Apple", "Orange", "Pear"]; *!* for (let key in arr) { */!* alert( arr[key] ); // Apple, Orange, Pear } ``` But that's actually a bad idea. There are potential problems with it: 1. The loop `for..in` iterates over *all properties*, not only the numeric ones. In the browser as well as in other environments, there are many collections of elements that *look like arrays*. That is, they have `length` and indexes properties, but they have *other non-numeric properties too*, which we usually don't need. The `for..in` loop will list them. If we need to work with arrays and those array-like structures, then these "extra" properties can become a problem. 2. The `for (let i=0; i