Table of contents
- How to initialize an array?
- push(): Add Element to an Array :
- pop(): Remove the Element form the array :
- length(): To check the length of an array :
- concat(): To add multiple arrays
- slice(): To slice out a piece of an array
- splice(): To add new items to the array
- sort(): To sort the array alphabetically
- reverse(): To reverse an array
- shift(): Used to remove the element from an array
- unshift(): Used to add element in an array
This is a brief guide about the methods of arrays in Javascript, we'll look at them one by one, but first, let's take a look at what an array actually is.
An array is just a variable that is a collection of multiple values or you can say an array is a variable that can hold multiple values.
Now the question is, what is the method? Simply, the methods are built-in functions of an array that help us manipulate an array in many ways. We'll take a look at these methods one by one, so let's start by creating an array.
How to initialize an array?
You can initialize or create a new array in many ways, one of the methods is:
let newArray = ["Bitcoin", "Ethereum", "Litecoin", "Solana", "Monero"];
push(): Add Element to an Array :
You can use the array's built-in method push() to add elements to an array. push() will add the element at the very end of an array.
let newArray = ["Bitcoin", "Ethereum", "Litecoin", "Solana", "Monero"];
newArray.push("Cardona"); // The new value "Cardona" gonna add at the end of an array.
console.log(newArray);
// Result :
[ 'Bitcoin', 'Ethereum', 'Litecoin', 'Solana', 'Monero', 'Cardona' ]
pop(): Remove the Element form the array :
You can use the array's built-in method pop() to remove the element from the array. pop() will remove the element from the very end of the array and will return the element that has been popped out of the array.
let newArray = ["Bitcoin", "Ethereum", "Litecoin", "Solana", "Monero"];
newArray.pop(); // Will remove "Monero" in this case and return the same value.
console.log(newArray);
// Result :
Monero
length(): To check the length of an array :
You can check the length of your array using the length() method of an array.
let newArray = ["Bitcoin", "Ethereum", "Litecoin", "Solana", "Monero"];
console.log(newArray.length);
// Result :
5
concat(): To add multiple arrays
You can use concat() method to concatenate/add one or more arrays.
let Array1 = ["Bitcoin", "Ethereum", "Litecoin", "Solana"];
let Array2 = ["Matic", "Monero"];
Array1.concat(Array2)
console.log(Array1.concat(Array2));
// Result :
[ 'Bitcoin' 'Ethereum', 'Litecoin', 'Solana', 'Matic', 'Monero']
slice(): To slice out a piece of an array
You can slice out a piece from an array using the "slice()" method and it will create a new array of that particular piece.
Slice() method doesn't modify the original array
let Array1 = ["Bitcoin", "Ethereum", "Litecoin", "Solana"];
Array2 = Array1.slice(1);
/*This will slice out pieces from the element index of "1" to the very
end of the array */
console.log(Array2);
// Result :
[ 'Ethereum', 'Litecoin', 'Solana' ]
slice() method can take up to two arguments.
slice(arg1, arg2)
This method will select elements from the start argument, and up to (but not including) the end argument.
let Array1 = ["Bitcoin", "Ethereum", "Litecoin", "Solana"];
Array2 = Array1.slice(1,3);
console.log(Array2);
// Result :
[ 'Ethereum', 'Litecoin' ]
splice(): To add new items to the array
You can add new items to the array using the splice() method.
The splice() method retruns an empty array.
splice(start, deleteCount, item1, item2, itemN)
let Array1 = [ "Bitcoin","Solana", "Litecoin", "Ethereum" ];
Array1.splice(1, 0, "Cardano", "Tron")
console.log(Array1);
// Result :
[ 'Bitcoin', 'Cardano', 'Tron', 'Solana', 'Litecoin', 'Ethereum' ]
console.log(Array1.splice(1, 0, "Cardano", "Tron"));
// Result :
[] // Returns empty array
sort(): To sort the array alphabetically
The sort() method is used to sort an array alphabetically and numerically.
Sorting alphabetically
let Array1 = [ "Bitcoin","Solana", "Litecoin", "Ethereum" ];
Array1.sort()
console.log(Array1.sort());
// Result :
[ 'Bitcoin', 'Ethereum', 'Litecoin', 'Solana' ]
Sorting Numerically
let Array2 = [2, 7, 1, 4, 6, 9]
Array2.sort()
console.log(Array2.sort());
// Result :
[ 1, 2, 4, 6, 7, 9 ]
reverse(): To reverse an array
The reverse() method is used to reverse the order of the elements in an array
let Array1 = ["Bitcoin","Solana", "Litecoin", "Ethereum"];
Array1.reverse()
console.log(Array1.reverse());
// Result :
[ 'Ethereum', 'Litecoin', 'Solana', 'Bitcoin' ]
shift(): Used to remove the element from an array
The shift() method is used to remove the first element in an array and will return the removed element.
shift() method changes the original array
let Array1 = ["Bitcoin","Solana", "Litecoin", "Ethereum"];
Array1.shift()
console.log(Array1.shift());
// Result :
Bitcoin
unshift(): Used to add element in an array
The unshift() method is used to add new elements to the beginning of an array and will return the new length of an array.
unshift() method changes the original array
let Array1 = ["Bitcoin","Solana", "Litecoin", "Ethereum"];
Array1.unshift("cardano")
console.log(Array1.unshift("cardano"))
// Result :
5
console.log(Array1)
// Result :
[ 'cardona', 'Bitcoin', 'Solana', 'Litecoin', 'Ethereum' ] // Modified Array
These are some of the most commonly used methods of an array, In a later article, we'll look at many more methods of arrays. I hope this article will help you to understand how the methods of array work.