JavaScript 101 /arrays
Arrays
Arrays allow you to store multiple values in a single variable. They are like lists that can hold different types of data, such as numbers, strings, or even other arrays.
You can also create an array by using square brackets [] and separating the values with commas. You can access the values in an array using their index, which starts at 0. The index goes between square brackets., like so: arrayName[0]
Let's manipulate some arrays
Some array methods change the original array (like push, pop, splice), while others return a new array or value without changing the original (like slice, concat, join).
The basics
arrayName.push(value, ...)- Adds one or more values to the end of the arrayarrayName.pop()- Removes the last value from the arrayarrayName.shift()- Removes the first value from the array
Intermediates
arrayName.unshift(value, ...)- Adds one or more values to the beginning of the arrayarrayName.slice(start, end)- Copies a portion of the array and returnsReturn: Sends the data back to the position where it was called so it can be captured or used itarrayName.splice(start, deleteCount)- Cuts a portion of the array and returns itarrayName.splice(start, deleteCount, item1, item2, ...)- Cuts a portion of the array, returns it and then inserts new elementsarrayName.concat(array2, ...)- Merges two or more arrays and returns a new arrayarrayName.join(separator)- Joins all elements of an array into a string, separated by the specified separator Separator: The string to separate each element from eachother in the resulting string. i.e. ", "
Gather info about arrays
arrayName.length- Returns the number of values in the arrayArray.isArray(value)- Checks if the value is an array - the way this is written is a bit weirdarrayName.indexOf(value)- Returns the index of the first occurrence of a value in the arrayarrayName.includes(value)- Checks if a value exists in the array