Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Basic Array Operations In JavaScript.
JavaScript arrays are a powerful data structure that allows you to store and manipulate collections of data. In this article, we will cover some of the basic operations that you can perform on arrays in JavaScript.
Table of Contents
Basic Array Operations In JavaScript
The first operation that we will cover is adding elements to an array. You can add an element to the end of an array using the push() method. The push() method takes one or more arguments and adds them to the end of the array. For example:
Code For push() Method
let myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray);
Output
[1, 2, 3, 4]
You can also add an element to the beginning of an array using the unshift() method. The unshift() method takes one or more arguments and adds them to the beginning of the array. For example:
Code For unshift() Method
let myArray = [1, 2, 3];
myArray.unshift(0);
console.log(myArray);
Output
[0, 1, 2, 3]
The next operation that we will cover is removing elements from an array. You can remove an element from the end of an array using the pop() method. The pop() method removes the last element of the array and returns it. For example:
Code For pop() Method
let myArray = [1, 2, 3];
let lastElement = myArray.pop();
console.log(lastElement);
console.log(myArray);
Output
3,
[1, 2]
You can also remove an element from the beginning of an array using the shift() method. The shift() method removes the first element of the array and returns it. For example:
Code For shift() Method
let myArray = [1, 2, 3];
let firstElement = myArray.shift();
console.log(firstElement);
console.log(myArray);
Output
1
[2, 3]
Another important operation that you can perform on arrays is to access elements at a specific index. You can access an element at a specific index using the square brackets []. For example:
let myArray = [1, 2, 3];
console.log(myArray[1]);
Output
2
You can also change the value of an element at a specific index by assigning a new value to the index. For example:
Code For index Method
let myArray = [1, 2, 3];
myArray[1] = 4;
console.log(myArray);
Output
[1, 4, 3]
Finally, you can also iterate over all elements in an array using a for loop or forEach method. For example:
Code For forEach Method
let myArray = [1, 2, 3];
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
or
let myArray = [1, 2, 3];
myArray.forEach(function(element) {
console.log(element);
});
One common task is to search for an element in an array. You can use the indexOf() method to find the first index of an element in an array. The indexOf() method takes one argument: the element you’re looking for. If the element is not found, the method returns -1. For example:
Code For indexof() Method
let myArray = [1, 2, 3, 4, 5];
console.log(myArray.indexOf(3));
console.log(myArray.indexOf(6));
Output
2
-1
Another useful method is the lastIndexOf() method, which is similar to indexOf() but it starts searching from the end of the array.
Code For lastIndexof() Method
let myArray = [1, 2, 3, 4, 3];
console.log(myArray.lastIndexOf(3));
Output
4
You can also use the join() method to convert an array to a string. The join() method takes one optional argument: the separator. If no separator is provided, the method uses a comma by default. For example:
Code For join() Method
let myArray = [1, 2, 3];
console.log(myArray.join());
console.log(myArray.join("-"));
Output
"1,2,3"
"1-2-3"
Another important operation that can be performed on arrays is to reverse the order of elements. You can use the reverse() method to reverse the order of elements in an array. This method modifies the original array and doesn’t return anything. For example:
Code For reverse() Method
let myArray = [1, 2, 3];
myArray.reverse();
console.log(myArray);
Output
[
]
3, 2, 1
You can also use the sort() method to sort the elements of an array. By default, the sort() method sorts the elements of an array in lexicographic order. For example:
let myArray = [3, 2, 1];
myArray.sort();
console.log(myArray);
Output
[
1, 2, 3
]
You can also use the filter() method to filter elements of an array based on some condition. For example:
let myArray = [1, 2, 3, 4, 5, 6];
let filteredArray = myArray.filter(function(element) {
return element > 3;
});
console.log(filteredArray);
Output
[4, 5, 6]
Conclusion
In conclusion, arrays are a versatile and powerful data structure in JavaScript. They allow you to store and manipulate collections of data and perform a wide range of operations on them.
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.