Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Introduction To Arrays In JavaScript.
Table of Contents
What Is Arrays ?
Arrays in JavaScript are a data structure that allows you to store and access a collection of items. They are similar to lists in other programming languages and can be used to store any type of data, including numbers, strings, and objects.
To create an array in JavaScript, you use the square brackets [] and list the items you want to store inside, separated by commas. For example:
Code For Arrays In JavaScript
let myArray = [1, 2, 3, 4, 5];
This creates an array called “myArray” that contains the numbers 1, 2, 3, 4, and 5.
You can also create an empty array and add items to it later using the push() method:
let myArray = [];
myArray.push(1);
myArray.push(2);
myArray.push(3);
You can access individual items in an array using the index of the item. Array indexes start at 0, so the first item in an array is at index 0, the second item is at index 1, and so on.
let myArray = [1, 2, 3, 4, 5];
console.log(myArray[0]);
console.log(myArray[2]);
Output
1
3
You can also change the value of an item in an array by assigning a new value to the index:
let myArray = [1, 2, 3, 4, 5];
myArray[2] = 6;
console.log(myArray);
Output
[1, 2, 6, 4, 5]
You can also use a for loop to iterate over all items in an array:
let myArray = [1, 2, 3, 4, 5];
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
JavaScript also provides several built-in array methods, such as:
.pop()
which removes the last element of an array.shift()
which removes the first element of an array.sort()
which sorts the elements of an array.filter()
which creates a new array with all elements that pass the test implemented by the provided function.map()
which creates a new array with the results of calling a provided function on every element in the calling array
In conclusion, arrays are an essential data structure in JavaScript. They are a powerful tool for storing and manipulating collections of data, and the built-in array methods make it easy to perform common tasks such as sorting, filtering, and transforming data.
Add Or Remove Item From Middle of An Array
One common task is to add or remove items from the middle of an array. You can use the splice() method to do this. The splice() method takes three arguments: the index at which to start changing the array, the number of items to remove, and the items to add.
For example, to add the number 4 at index 2 and remove the number 3, you can use the following code:
let myArray = [1, 2, 3, 5, 6];
myArray.splice(2, 1, 4);
console.log(myArray);
Output
[1, 2, 4, 5, 6]
Slice() Method In JavaScript
Another useful method is the slice() method, which allows you to extract a portion of an array and return it as a new array. The slice() method takes two arguments: the start index (inclusive) and the end index (exclusive).
For example, to extract the subarray [4, 5], you can use the following code:
let myArray = [1, 2, 3, 4, 5, 6];
let subArray = myArray.slice(3, 5);
console.log(subArray);
Output
[4, 5]
You can also use the concat() method to combine two or more arrays. This method returns a new array that contains all the elements from the original arrays. For example:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let combinedArray = array1.concat(array2);
console.log(combinedArray);
Output
[1, 2, 3, 4, 5, 6]
JavaScript also provides a number of built-in functions to work with arrays such as .forEach()
, .reduce()
, .some()
, .every()
, .includes()
etc.
For example, .forEach()
method is used to execute a function for each element in an array. The function takes three arguments: the current value, the current index, and the array.
let myArray = [1, 2, 3, 4, 5];
myArray.forEach(function(value, index, array) {
console.log(value, index, array);
});
Conclusion
In conclusion, arrays are a versatile and powerful data structure in JavaScript. They allow you to store and manipulate collections of data, and the built-in array methods and functions make it easy to perform a wide range of tasks.
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.