Javascript Array: Understanding the Basics
15 January, 2023
1
1
1
Contributors
An array is a collection or stack of various data. The way to write an array is with square brackets ([]), and its elements are separated using commas (,). Each element of the array has an index that starts from 0, 1, 2 and so on, to access the index, just use square brackets along with the index, for example array[0] (which means array with index 0).
Here is an illustration of an array
array illustration
if inserted into javascript code, it would look like this
If we look at the illustration above, the index 2 of the array has a value of 3, so the result of array[2] is 3 and when we give index 0 to the array (array[0]), we will get the result of 1.
Arrays can also be manipulated in various ways such as adding and removing elements in the array, merging arrays, or even deleting all elements of the Array. We can insert various types of data into the Array even insert Array into Array.
Array also has a property .length like strings which means the length of an array.
Array methods
•
.push()
Push is an array method that adds a value at the end of the last element in an array. The push method takes a single parameter which is the value that we want to add to the array.
•
.pop()
Pop is an array method used to remove the last element from an array. This method does not require any parameters, so it will only remove one element, which is the last element of the array.
•
.unshift()
Unshift is a method in JavaScript that adds an element to the beginning of an array. It takes one or more elements as its parameter and adds them to the front of the array. This method changes the length of the array and also shifts all existing elements to the right. Unshift is the opposite of the shift method which removes an element from the beginning of an array.
•
.shift()
Shift is a method in JavaScript arrays used to remove the first element of an array and return that element. This method does not take any parameters and modifies the original array by removing the first element. It is the opposite of the unshift method, which adds an element to the beginning of an array.
•
.sort()
Sort is a method for sorting values in an array. By default, sort will sort in ascending order and based on the unicode of the characters. Unicode order means that there are characters that have a greater value compared to other characters. For example, the character "b" is greater than "a", the character "c" is greater than the character "b", etc.
•
.slice()
The slice() method in JavaScript is used to extract a part of an array and return it as a new array, without modifying the original array. The slice() method takes two arguments, the first is the start index and the second is the end index. The elements in the new array will be the elements from the start index up to, but not including, the end index. For example:
•
.splice()
The splice() method in JavaScript is used to add or remove elements from an array. It takes three arguments: the index at which to start changing the array, the number of elements to remove, and the elements to add.
You can also use splice() to remove elements without adding new elements, for example:
You can also use splice() to add multiple elements at once, for example:
•
.split() and .join()
The split method is used to split a string into an array. It takes a parameter, which is the separator character used to divide the string.
The join() method in JavaScript is used to join all elements of an array into a string. The method takes one optional parameter, which is the separator used to join the elements. If no separator is provided, a comma is used by default.
Summary
JavaScript arrays are a collection of data that can be manipulated in various ways, such as adding and removing elements, combining arrays, or even deleting all elements of an array. Different data types can be inserted into an array, including arrays within arrays. Arrays also have a property called .length, similar to strings, which represents the length of an array.
There are several methods available to manipulate arrays in JavaScript, such as push, which adds a value to the end of an array, and pop, which removes the last value of an array. The unshift method adds a value to the beginning of an array, while the shift method removes the first value of an array. The sort method sorts the values in an array, and the slice and splice methods allow for the extraction of specific elements of an array. The split method is used to split a string into an array and the join method is used to join elements of an array into a string. Each of these methods have specific uses and applications to help manipulate and manage arrays in JavaScript.