Introduction
In the JavaScript array is nothing but an object and that grows dynamically means when you push or delete the element from array JS engine will automatically handle. initially when you create an array by default size will be zero and as soon as you will add/delete values it will be keep increasing/decreasing as per the requirement. When the array reaches its current capacity, the engine allocates a new, larger array (usually double the current size) and copies the existing values to the new array. This helps to minimize the number of times the array needs to be resized. Array values start from zero means when you add first value in array to access that we need to use array [0]
for next value it will array [1]
and so on.
How to create Array and access in JS?
//One way
//Length will be zero for below array
var array1 = new Array();
//2nd way
//Length will be 3 for below array
var array2 = [2,3,4];
//How to access value from array
console.log(array2[0]); // output = 2 here console log is use to print the array value
//OR
console.log(array2.at(1));
Will an array throw an ArrayIndexOutOfBoundsException
in JavaScript? What if you try to access using string, -1, float values?
In JS array will not throw arrayIndexOutOfBound instated of this it will return undefined. even if you try to access using string, -1 or float for everything it will return undefined. Basically, when you invalid index it will give undefined. and this how JS is designed,
var array = [2,3,4,5];
console.log(array[9]); // output - undefined
//what if you try to access using -1 🤔
console.log(array[-1]); // output - undefined
//what if you try to access using "232" 🤔
console.log(array["1"]; //output - undefined
//what if you try to access using 1.1 🤔
console.log(array[1.1];
Most Used Array Method
Arrays.filter()
This method is used to filter the values from the array. and return type will be array.
Ex -: let’s assume you one person name as “changu” and in his family total 6 family member. “Hathi Lal”, “Chubhul panday”, “Crime Master Gogo”, “Babu Bhaiya”, “Virus”, “Gadha Prasad”. Changu wants to organize one party, but he wants to invite only those whose name length is less than 9, so what we can do is
var changu_family = ["Hathi Lal", "Chubhul panday", "Crime Master Gogo","Gadha Prasad", "Babu Bhaiya", "Virus"];
// member is variable name you can give anything whatever you want
var family = changu_family.filter((member)=> member.length<=12);
console.log(family);//output - [ 'Hathi Lal', 'Gadha Prasad', 'Babu Bhaiya', 'Virus' ]
Array.includes()
Using this method, we can check like is any specific value present in your array or not and if value is present, it will return
true
elsefalse
.Ex -: Let’s take our previous example has short term memory loss, but he is expert in JS someone came and asked “chin tapak dam dam” is your family member? for that he used
includes ()
method find out.
var changu_family = ["Hathi Lal", "Chubhul panday", "Crime Master Gogo","Gadha Prasad", "Babu Bhaiya", "Virus"];
//Here basically we are searching if this person present in the array or not
var isMyFamilyMember = changu_family.includes("chin tapak dam dam");
console.log(isMyFamilyMember); // output - false
Array.toSorted()
Using this method, we can sort the array but please keep in mind it will not touch your original array first it will create copy of the array and then it will sort the array.
Note -:
1-: This method will not work in older Node.js version 18 and below.
2-: When you use arr.toSorted()
without using compare function it will treat every array as String which can lead to unexpected result if you are using for number, please use compare function to handle this.
3-: arr.toSorted((a, b) => a-b);
Please here a and b are variable on place of a and b you can use anything
After doing a-b if value/result is |
+ve (Its mean a is greater than b) |
-ve (Its mean b is greater than a) |
zero (Its mean a and b both are equal) |
var changu_family = ["Hathi Lal", "Chubhul panday", "Crime Master Gogo","Gadha Prasad", "Babu Bhaiya", "Virus"];
var sorted_family = changu_family.toSorted();
console.log(sorted_family) // [ 'Hathi Lal','Chubhul panday','Crime Master Gogo','Gadha Prasad','Babu Bhaiya','Virus'];
//Now using compare function
var changu = [1,2,3,4,4,5321,23,12321,432];
var numbers = changu.toSorted((a,b)=> a-b);
console.log(numbers); // [1,2,3,4,4,23,432,5321,12321]
Arrays.sort()
This method is also used to sort the array but here difference is it will work on your original array.
and one thing is common in both method is both of them by default treat array as String, so it’s recommended if you want to use for number always use compare function for correct result.
//Without using compare function for number var changu = [1,2,3,4,4,5321,23,12321,432]; changu.sort(); console.log(changu); // [1, 12321,2,3,4,4,432, 5321] //With use of compare function var changu = [1,2,3,4,4,5321,23,12321,432]; changu.sort((a,b) => a-b); console.log(changu); // [1,2,3,4,4,23,432,5321,12321]
Arrays.every()
This method is used to check if all the elements pass the condition that is given in function and every method return
true or false
means all the element of the array pass the condition it will return true else it will return false.//One way of writing function function AllNumberGreaterThan20(value){ return value>20; } //one more way of writing function //const NumberAllGreaterThan20 = (currentValue) => currentValue > 20; //you can use any of them as per your requirements var changu = [5321,23,12321,432]; var isAllNumberGreaterThan20 = changu.every(AllNumberGreaterThan20); console.log(isAllNumberGreaterThan20); // output -: true
Arrays.some()
In this method we will check if any of the element/value pass the condition given in the function if single value pass the condition, it will return true else false.
// function AtLeastSingleValueGreaterThan20(value){
// return value>20;
// }
const AtLeastSingleValueGreaterThan20 = (value) => value > 20;
var changu = [5,23,1,4];
var isAtLeastSingleValueIsGreaterThan20 =changu.some(AtLeastSingleValueGreaterThan20);
console.log(isAtLeastSingleValueIsGreaterThan20); //output -: true
var changu = [5,3,1,4];
var isAtLeastSingleValueIsGreaterThan20 =changu.some(AtLeastSingleValueGreaterThan20);
console.log(isAtLeastSingleValueIsGreaterThan20); //output -: false
Arrays.unshift()
If you want to push element from starting of the array you can use this method. whenever you use this method to push the element it will insert in starting of the array.
var arr = [5,23,1,4];
//Now you wanted to add 8 at index 0
arr.unshift(8);
console.log(arr); // 8, 4, 23, 1, 4
arr.unshift(10);
console.log(arr); // 10, 8, 4, 23, 1, 4