JavaScript Array
JavaScript array is a data structure that allows you to store and manipulate collections of values or elements. It is a special type of object that holds a list of values, which can be of any data type, such as numbers, strings, objects, or other arrays.
Arrays in JavaScript are zero-indexed, which means that the first element in an array is located at index 0, the second element is located at index 1, and so on. You can access and modify array elements using their index number.
Here is an example of how to create an array in JavaScript.
//Declaring an empty array using Array() constructor and array literal notation []
let emptyArr = new Array();
let anotherEmptyArr= [];
// array of numbers
let numbers = [1, 2, 3, 4, 5];
// array of strings
let fruits = ["apple", "banana", "orange"];
// array of different data types
let mixed = [1, "apple", true, null];
You can also modify an element in an array by assigning a new value to its index number
let numbers = [1, 2, 3, 4, 5];
// changing the value of the second element in the array
numbers[1] = 10;
alert(numbers); // output: [1, 10, 3, 4, 5]
Array methods push(), pop(), shift(), unshift()
Push()
The push() is a built-in method of an array that adds one or more elements to the end of the array and returns the new length of the array.
Here's an example of push() method in JavaScript.
let fruits = ['apple', 'banana', 'orange'];
// adding a new fruit to the end of the array
fruits.push('plum');
alert(fruits); // output: ['apple', 'banana', 'orange', 'plum']
Pop()
The pop() is a built-in method of an array that removes the last element from the array and returns that element.
Here's an example of pop() method in JavaScript.
let fruits = ['apple', 'banana', 'orange'];
// remove the last fruit from the array
fruits.pop();
alert(fruits); // output: ['apple', 'banana']
Shift()
The shift() is a built-in method of an array that removes the first element from the array and returns that element.
Here's an example of shift() method in JavaScript.
let fruits = ['apple', 'banana', 'orange'];
// removing the first fruit from the array
fruits.shift();
alert(fruits); // output: ['banana', 'orange']
Unshift()
The unshift() is a built-in method of an array that adds one or more elements to the beginning of the array and returns the new length of the array.
Here's an example of unshift() method in JavaScript.
let fruits = ['banana', 'orange'];
// adding a new fruit to the beginning of the array
fruits.unshift('apple');
alert(fruits); // output: ['apple', 'banana', 'orange']
Multidimensional Array
Multidimensional array is an array that contains one or more arrays as its elements. Each array within the multidimensional array is a separate dimension or level of the array, and the elements of each array can be accessed by specifying both the index of the array and the index of the element within that array.
Here's an example of multidimensional array in JavaScript.
const myArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// example on how to access data from multidimensional array
const data = myArray[1][2];
alert(data); // Output: 6
In this example, myArray is a two-dimensional array with three rows and three columns.
To access the elements of this array, you need to specify both the index of the row and the index of the column. For example, to access the data in the second row and third column (which has a value of 6), you can use the following code above.
You can also use the same 4 methods and other methods that is built-in in an array to manipulate multidimensional array.
Here's an example projects implementing those 4 array methods in JavaScript.
Conclusion
Overall, All four of these methods modify the original array, but they return a value that you can use if needed. By using these methods, you can add, remove, or manipulate elements in an array without having to manually reassign the entire array.
Now challenge yourself, click the link below to practice!