The syntax of an array is as follows:
const arr_name = [first_item, second_item, third_item,............., last_item];
- They can be resized. That means the length can be increased through several methods, such as push.
- They can be a combination of multiple data types
- They are not associative, which implies it is impossible to access them using arbitrary strings but, instead, can be accessed through positive integers as indexes.
- Their copy operations result in shallow copies, implying that they share the same properties with the parent source.
Creating an Array
There are several ways of creating a new array, with the word ‘new’ or without. Notably, both approaches will create your desired array. That is illustrated as follows;
- Using the Array constructor
const cars = new Array ("Toyota", "Volvo", "Audi", "BMW"); console.log(cars)
["Toyota", "Volvo", "Audi", "BMW"]
- Using the literal notation.
Similarly, the same array can be created using the literal notation as follows:
const cars = ["Toyota", "Volvo", "Audi", "BMW"]; console.log(cars)
["Toyota", "Volvo", "Audi", "BMW"]
- Using the Array constructor containing one parameter. The length property of an array comes into play when constructing an array using this method. The array is formed by inputting the array's length as the parameter.
const cars = new Array (4)
Assess the length of the array: console.log(cars. Length);
Notably, there are no elements in the array, and accessing the index of any of them will output undefined.
How to Access an Element in an Array.
const cars = ["Toyota", "Volvo", "Audi", "BMW"];
console.log(0); console.log(1); console.log(2); console.log(3);
Toyota Volvo Audi BMW
The following are some of the methods that you may encounter in your development journey. Every method works differently from each other and is designed to perform specific functions on arrays.
The method confirms whether a given value is an array or not. For instance, you may want to confirm if the above example of cars is an array. The return value is a Boolean value, either false or true.
The join method is used to create a string through concatenation of all the elements contained inside the array. In the event that you have multiple elements in your array, the return values are separated using a comma.
The join method example is illustrated as follows.
const students= ['Jane', 'John', 'Peter']; console.log(students.join()); // "Jane,John,Peter"
The joining of the elements can be done in various ways. This is illustrated in the code below:
students. Join(); //"Jane,John,Peter" students.join(', ');//”Jane, John, Peter” students.join(' + '); //”Jane + John + Peter” Students.join (''); //”JaneJohnPeter”
The map method creates a new array, which is the output of the call of a given function, on each element contained in the array. For instance, you may wish to create a new array that contains squared values from the original array's elements. That can be achieved using the map function.
const array1 = [1, 2, 3, 4]; const array2 = array1.map(x=>x**2); console.log(array2); // [ 1, 4, 9, 16 ]
The pop method pops out or rather, removes the last element of a given array. It is important to note that the method changes the length of the original array; the returned value is the popped element and not the new array. Where the array is empty, the returned value is always undefined.
The new array does not contain the ‘ReactJs’ element
The push method adds a new element or multiple elements to the end of an existing array. The returned value contains the new array with added elements. For instance, we may wish to add two more skills to the stack array using the example above.
Add TypeScript and C
The slice method returns a portion or a shallow copy of the original array, depending on where the start and the end have been specified. Notably, the index of the given items is specified to imply the beginning and end. However, it is essential to note that no modifications are made to the array due to the operation of the slice method.
The method is further explained in the code examples below:
const subjects= ['Biology', 'Chemistry', 'English', 'Physics', 'Religious Studies']; console.log(subjects. Slice(2));
The code example returns ['English,' 'Physics,' 'Religious Studies'] because the start index specified was 2, which implies that it slices from English. However, no end condition was specified, meaning that it would return the rest of the array.
The example returns an array from English to Physics. That might not be very clear, but it is important to note that the final index specified does not become part of what is returned. The operation above returns the element in indexes 2 and 3, which is ['English', 'Physics'].
The code returns the element in index 2, which is English, and the second last element, which is Physics.
The shift method is the opposite of pop, which removes the last element. Therefore, we expect the shift() method to act completely oppositely. It removes the first element in a given array and returns it. Similarly, it results in a reduction of the overall length of the array.
The method is used to merge two or more arrays into a new one, which is the return value. It is important to note that the method differs from the join method, which returns a string.
The method returns elements in an existing array that meet the specified criteria or pass the condition due to the execution of a given function. It maps through all the elements and returns the elements that meet a given condition.