Discovering Arrays in Javascript with beautiful examples (Part -1)
["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
Don't worry, we will not start with this ๐
Arrays: The collection of different types of data/elements inside [ ] is called Arrays. In Javascript, the array is a single variable that stores multiple elements. It can be a boolean, string, object, number, etc.
Declaration
There are 2 ways to declare an array inside js.
let array1= []; // Method-1 This one is mostly preferred.
let array2= new Array(); // Method-2 This one is optional.
Initialization
let array1=['A', 'B', 'C', 'D', 'E' ];
let array2 = new Array(5); // Creates 5 undefined elements inside array2
How to access elements inside Array
He is saying right. We use the array index to access elements inside the array, which starts from 0.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals[0]); // ๐ซ
console.log(animals[1]); // ๐ฆ
console.log(animals[2]); // ๐ฆ
console.log(animals[3]); // ๐ฟ
// The index of an array's last element is always one
// less than the length of the array.
Ok, so till here I understand what is array and how is declare it and initialize it. But if we have millions or many elements inside the array, how do we count them? ๐ค๐ค
Here comes Array. length property. Let's understand the same example of animals.
let animals=["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals.length); // 7
Array Methods
Array.includes()
If you want to check whether that particular element is present inside your array or not, this method is used.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals.includes("๐ก")); // true
console.log(animals.includes("๐ฆข")); // false
Array.indexof()
If you don't know at which index, ๐ฆ is stored. To find that out, we use this method.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals.indexof ("๐ฆ")); // 2
Array.push()
If you want to add/append an item inside your array from the last end, you can use this method.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals.push("๐"));
//Output : ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก", ๐"]
Note: Using this method our original array does not get changed, it only just appends that item.
Array.unshift()
If you want to add an item to the top of the array, you can use this method.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals.unshift("๐"));
//Output : ["๐", ๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
Array.pop()
This method is used to remove the last element of your array. It doesn't take any parameters.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ]
console.log(animals.pop("๐ก"));
//Output : [๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ]
Array.shift()
If you want to remove an item from an array from the starting point, you can use this method.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ]
console.log(animals.shift());
//Output : [ "๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ]
Array.at()
If you want to know the array item at a particular index, use this method. It also accepts the negative index which starts from the right.
let animals= ["๐ซ" ,"๐ฆ" ,"๐ฆ" ,"๐ฟ" ,"๐ฆ" ,"๐ณ" ,"๐ก"]
console.log(animals.at(2));
//Output : "๐ฆ"
Array.concat() This method is used to merge two or more arrays. Here also, the original array doesn't get changed, although returns a new array.
let smile = ["๐" , "๐คฃ" , "๐"];
let sad = ["๐ฅ" , "๐", "โน"];
let neutral =smile.concat(sad); // ["๐" , "๐คฃ" , "๐", "๐ฅ" , "๐", "โน"];
Array.every() This method executes the function on each array element and returns a boolean value.
let even= [2,4,6,8,10];
let checkEven= function(num){
if(num%2==0){
return num;
}}
even.every(checkEven);
Output :
Array.join() This method returns a new string that combines all the elements inside the array. These elements can be separated either by comma or any specified separator string. So if you want to convert your array into a string, use this method.
let smile = ["๐" , "๐คฃ" , "๐"];
smile.join(); // '๐,๐คฃ,๐'
smile.join(" "); // '๐ ๐คฃ ๐'
smile.join("_"); //'๐_๐คฃ_๐'
Array.split() This method is used to convert a string into an array. It is opposite of join method. ``` let myString= " India, Bhutan, Malaysia, Germany, USA, China, UK "; let myAray= myString.split(",");
// Output: [' India', ' Bhutan', ' Malaysia', ' Germany', ' USA', ' China', ' UK '] ```