Introduction
JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects. Some of the important methods used with arrays are discussed in this article.
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.
- <script>
- let developer = ['arvid', 'soniya', 'priya', 'rahul'];
- let testor = ['umesh', 'kalpana', 'suresh', 'zoya'];
- let employee = developer.concat(testor);
- console.log(employee)
- </script>
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- let iterator = employee.entries();
- for (let e of iterator) {
- console.log(e)
- }
- </script>
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- console.log(employee.filter(e => e.length < 6));
- console.log(employee.filter(e => e.length > 6))
- console.log(employee.filter(e => e.length == 6))
- </script>
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.
- <script>
- let numbers = [10, 12, 15, 18, 9, 5, 22, 13, 30];
- let found = numbers.find(function (element) {
- return element > 10;
- });
- console.log(found);
- </script>
The fill() method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array.
- <script>
- let numbers = [10, 15, 12, 3, 4, 7, 2];
- console.log(numbers.fill(0, 1, 3))
- console.log(numbers.fill(5, 1))
- console.log(numbers.fill(6))
- </script>
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test.
- <script>
- let emplyees = ['Mariya', 'priya', 'sonam', 'juliya', 'priyanka'];
- console.log(emplyees.indexOf('priya'))
- </script>
The sort() method sorts the elements of an array in place and returns the array.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- const sortemployee = employee.sort();
- console.log(sortemployee)
- </script>
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- const reverseemployee = employee.reverse();
- console.log(reverseemployee)
- </script>
The toString() method returns a string representing the specified array and its elements.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- console.log(employee.toString())
- </script>
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- console.log(employee.shift())
- </script>
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- let unsiftemployee = employee.unshift('afra', 'farhan');
- console.log(employee)
- </script>
The push() method adds one or more elements to the end of an array and returns the new length of the array.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- let newemployee = employee.push('Buvi', 'Chethan');
- console.log(employee)
- </script>
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
- <script>
- let employee = ['arvid', 'soniya', 'priya', 'rahul', 'umesh', 'kalpana', 'suresh', 'zoya'];
- let deleteemployee = employee.pop('zoya');
- console.log(employee)
- </script>
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
- <script>
- let table = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- console.log(table.map(x => x * 2));
- </script>