Hello Coders... Welcome back !!! Hope you are doing absolutely well and giving much time to tech. I gave some of my time to first preparing this and writing this one. Today going to write about arrays and it's methods in Javascript. So let's start the topic.
What is Array?
An array is a special variable, which can hold more than one value. In other words, array is an object that can store multiple values at once.
For Example
const words = ['hello', 'world', 'welcome'];
Here, words is an array. The array is storing 3 values.
Creating an Array
We can create an array using two ways:
1. Using an array literal
The easiest way to create an array is by using an array literal []
. For example,
const array1 = ["eat", "sleep"];
2. Using the new keyword
We can also create an array using JavaScript's new
keyword.
const array2 = new Array("eat", "sleep");
In both of the above examples, we have created an array having two elements.
We can also store arrays, functions and other objects inside an array. For example,
const newData = [
{'task1': 'exercise'},
[1, 2 ,3],
function hello() { console.log('hello')}
];
Array Method
There are many important array methods. Let's discuss them one by one.
push()
The push()
method adds an element at the end of the array. For example,
let dailyActivities = ['eat', 'sleep'];
// add an element at the end
dailyActivities.push('exercise');
console.log(dailyActivities); // ['eat', 'sleep', 'exercise']
unshift()
The unshift()
method adds an element at the beginning of the array. For example,
let dailyActivities = ['eat', 'sleep'];
//add an element at the start
dailyActivities.unshift('work');
console.log(dailyActivities); // ['work', 'eat', 'sleep']
pop()
We can use the pop()
method to remove the last element from an array. The pop()
method also returns the returned value. For example,
let dailyActivities = ['work', 'eat', 'sleep', 'exercise'];
// remove the last element
dailyActivities.pop();
console.log(dailyActivities); // ['work', 'eat', 'sleep']
// remove the last element from ['work', 'eat', 'sleep']
const removedElement = dailyActivities.pop();
//get removed element
console.log(removedElement); // 'sleep'
console.log(dailyActivities); // ['work', 'eat']
shift()
The shift()
method removes the first element and also returns the removed element. For example,
let dailyActivities = ['work', 'eat', 'sleep'];
// remove the first element
dailyActivities.shift();
console.log(dailyActivities); // ['eat', 'sleep']
length
We can find the length of an element (the number of elements in an array) using the length
property. For example,
const dailyActivities = [ 'eat', 'sleep'];
// this gives the total number of elements in an array
console.log(dailyActivities.length); // 2
concat()
The concat()
method creates a new array by merging (concatenating) existing arrays.
let primeNumbers = [2, 3, 5, 7]
let evenNumbers = [2, 4, 6, 8]
// join two arrays
let joinedArrays = primeNumbers.concat(evenNumbers);
console.log(joinedArrays);
/* Output:
[
2, 3, 5, 7,
2, 4, 6, 8
]
*/
isArray()
The isArray()
method checks whether the passed argument is an array or not.
let numbers = [1, 2, 3, 4];
// checking whether numbers is an array or not
console.log(Array.isArray(numbers));
let text = "JavaScript";
// checking whether text is an array or not
console.log(Array.isArray(text));
// Output:
// true
// false
join()
The join()
method returns a new string by concatenating all of the elements in an array, separated by a specified separator.
let message = ["JavaScript", "is", "fun."];
// join all elements of array using space
let joinedMessage = message.join(" ");
console.log(joinedMessage);
// Output: JavaScript is fun.
slice()
The slice()
method returns a shallow copy of a portion of an array into a new array object.
let numbers = [2, 3, 5, 7, 11, 13, 17];
// create another array by slicing numbers from index 3 to 5
let newArray = numbers.slice(3, 6);
console.log(newArray);
// Output: [ 7, 11, 13 ]
splice()
The splice()
method returns an array by changing (adding/removing) its elements in place.
let prime_numbers = [2, 3, 5, 7, 9, 11];
// replace 1 element from index 4 by 13
let removedElement = prime_numbers.splice(4, 1, 13);
console.log(removedElement);
console.log(prime_numbers);
// Output: [ 9 ]
// [ 2, 3, 5, 7, 13, 11 ]
values()
The values()
method returns a new Array Iterator object that contains the values for each index in the array.
let languages = ["JavaScript", "Java", "C++"];
// returns an Array Iterator object that contain values
let iteratorObject = languages.values();
// looping through iterator
for (let value of iteratorObject) {
console.log(value);
}
// Output:
// JavaScript
// Java
// C++
sort()
The sort()
method sorts the items of an array in a specific order (ascending or descending).
let city = ["California", "Barcelona", "Paris", "Kathmandu"];
// sort the city array in ascending order
let sortedArray = city.sort();
console.log(sortedArray);
// Output: [ 'Barcelona', 'California', 'Kathmandu', 'Paris' ]
There are some more array methods as well but we can explore them self.
Thanks for reading...
Reference- https://www.programiz.com/