Arrays are sequences of values that have a specific order and a length.
Values can be retrieved from an array using []. Indexes start at 0.
We can replace array elements with [] and =.
JavaScript Arrays: Stack
We can add elements to the end of an array with push. push returns the array's length, including the newly-pushed element.
pop is the opposite of push. It removes the last element from the array. pop returns the element that was removed.
Some array methods return a new array. push and pop do not. Instead, they change the array itself each time they're called.
JavaScript Arrays: For each
The forEach method executes a function once for each element in an array.
forEachlets us write the same code without the index variable i.
The forEach method always returns the value undefined and is not chainable.
We can modify the array's elements during the forEach.
JavaScript Arrays: Slice
If we want to access a subsection of an array we can use the slice method. It takes an argument begin, which is the index to start from.
slice can take a second argument, end. It slices all elements from begin up to end, but not including end.
We can slice beyond the end of the array. It gives the same result as slicing right up to the last element.
If our begin index is past the end of the array, we get an empty result.
With no arguments, slice will slice all elements of the array. This effectively copies the array. If we change the original, it won't affect the copy. Likewise, if we change the copy, it won't affect the original.
JavaScript Arrays: Slice with negative arguments
We can slice from the end of the array by using negative indexes. You can think of a negative index -2 as array.length - 2.
With negative indexes, we can also slice before the beginning of the array. The result will only include elements in the original array. It won't invent additional elements to satisfy our out-of-bounds index.
Both begin and end can be negative. Remember that the end element isn't included in the slice.
We can mix positive and negative begin and end indexes.
JavaScript Arrays: Map
The map method calls a function on each element of an array. It returns a new array of the values returned from those function calls.
map doesn't change the original array.
JavaScript Arrays: Concat
The concat method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
concat takes multiple arguments, so we can combine many arrays if needed
concat can also be used to add single elements to the end of an array. If its argument isn't an array, it will be added as a single element.
JavaScript Arrays: Includes
The includes method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
JavaScript Arrays: Arrays are objects
In JavaScript, arrays are a special type of object. We can see this by looking at typeof someArray, which will return 'object', just like it does for regular objects.
Fortunately, this doesn't affect normal uses of arrays. When we store values at the array's indexes and read them back, everything works normally.
Extra properties assigned to an array don't change its length.
In most cases, adding extra properties to arrays in this way is a mistake. It's better to use an object in this case.
JavaScript Arrays: Filter
filter calls a function on each element in an array. If the function returns true, then filter keeps that element. Otherwise, it discards the element.
span style="color: brown; background-color: rgb(194, 199, 199);">filter builds a new array. The original array isn't changed.
JavaScript Concurrency: setTimeout
Asynchronous code doesn't run all at once, from beginning to end. Instead, asynchronous code schedules other code to run in the future. (Asynchronous means "outside of normal execution order".)
In JavaScript, the simplest way to run code asynchronously is with setTimeout. When we call setTimeout(someFunction, 1000), we're telling the JavaScript runtime to call someFunction 1000 milliseconds (1 second) in the future.
JavaScript Concurrency: Concurrent setTimeouts
setTimeout can have an interval of 0, which means "do this immediately". However, it's still asynchronous! Our code will still finish before the setTimeout callback is called.
Timers are asynchronous regardless of how many we create. When we create 3 timers, each with a delay of 0, none will run until the current code finishes. Timers with a delay of 0 will always execute in the order that they were created.
The order of the setTimeout calls doesn't matter. If we switch their order, we'll get the same result: the timer with the shorter delay will fire first.
When a timer's callback is run, it begins, runs, and finishes, all without being interrupted. Then the JavaScript engine checks again. If there are other timers ready, they run.
JavaScript Arrays: Join
The join() method creates and returns a new string by concatenating all of the elements in an array
If we omit join's argument, the strings are joined with ',' by default.
join's argument is the string to put between each array element. It can be any string.
If we join some empty strings together, we get a string of only commas. Think about it as: the string has all of the commas in the original array.
null and undefined become empty strings when joined.
join can be used to build a large string using short lines of code.
JavaScript Arrays: Index of
We can ask an array for the indexOf a particular value. (Indexes start at 0, as usual.)
If the value occurs multiple times in the the array, we'll get the index of the first occurrence.
If we ask for an element that isn't in the array, we get -1.
JavaScript Arrays: Some and every
The some method decides whether a function is true of any element in an array. For example: "are any of these numbers two?" Or "are any of these strings empty?"
some always returns false for an empty array. This might seem strange, but it makes some sense.
every is true if our function is true for every element in the array.
every always returns true for an empty array. One way to remember this is that every's behavior for empty lists is the opposite of some's behavior.
some and every operate on any array, no matter how big it is.
JavaScript Arrays: flat and flatMap
By default, flat only flattens one level of arrays, just like our flatten function above did. Any arrays nested more deeply than one level are left unmodified.
If we want to flatten beyond the first level, we can provide a depth argument to flat. The default depth is 1, so the examples above used that value.
The depth can be Infinity, in which case flat will flatten all arrays no matter how deeply nested they are.
flatMap is identical to calling map followed by flat.
Both flat and flatMap build and return a new array. The original array remains unmodified.