- An Array is an object type designed for storing data collections.
- Key characteristics of JavaScript arrays are:
- Elements: An array is a list of values, known as elements.
- Ordered: Array elements are ordered based on their index.
- Zero indexed: The first element is at index 0, the second at index 1, and so on.
- Dynamic size: Arrays can grow or shrink as elements are added or removed. Why Use Arrays? If you have a list of items (a list of car names, for example), storing the names in single variables could look like this:
let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";
//instead of this we can use array
const cars = [ "Saab","Volvo","BMW"];
You can also create an empty array, and provide elements later:
const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";
Converting an Array to a String
The JavaScript method toString() converts an array to a string of (comma separated) array values.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
//output:Banana,Orange,Apple,Mango
JavaScript Array length:
The length property of an array returns the number of elements in the array. It automatically updates as elements are added or removed.
let a = ["HTML", "CSS", "JS", "React"];
console.log(a.length);//output:4
JavaScript Array join() Method:
This join() method creates and returns a new string by concatenating all elements of an array. It uses a specified separator between each element in the resulting string.
let a = ["HTML", "CSS", "JS", "React"];
console.log(a.join('|'));
//output:HTML|CSS|JS|React
JavaScript Array delete Operator:
The delete operator is used to delete the given value which can be an object, array, or anything.
let emp = {
firstName: "Riya",
lastName: "Kaur",
salary: 40000
}
console.log(delete emp.salary);
console.log(emp);
//output:
//true
//{"firstName":"Riya","lastName":"Kaur"}
JavaScript Array concat() Method:
The concat() method is used to concatenate two or more arrays and it gives the merged array.
let a1 = [11, 12, 13];
let a2 = [14, 15, 16];
let a3 = [17, 18, 19];
let newArr = a1.concat(a2, a3);
console.log(newArr);
//output:[11,12,13,14,15,16,17,18,19]
JavaScript Array flat() Method:
The flat() method is used to flatten the array i.e. it merges all the given array and reduces all the nesting present in it.
const a1 = [['1', '2'], ['3', '4', '5',['6'], '7']];
const a2 = a1.flat(Infinity);
console.log(a2);
//output:["1","2","3","4","5","6","7"]
Explanantion:The code defines a multilevel (nested) array 'a1' and uses the flat(Infinity) method to flatten it completely into a single-level array.
JavaScript Array.push() Method:
The push() method is used to add an element at the end of an Array. As arrays in JavaScript are mutable objects, we can easily add or remove elements from the Array.
let a = [10, 20, 30, 40, 50];
a.push(60);
a.push(70, 80, 90);
console.log(a);
//output:[10,20,30,40,50,60,70,80,90]
JavaScript Array.unshift() Method:
The unshift() method is used to add elements to the front of an Array.
let a = [20, 30, 40];
a.unshift(10, 20);
console.log(a);
//output:[10,20,20,30,40]
JavaScript Array.pop() Method:
The pop() method is used to remove elements from the end of an array.
let a = [20, 30, 40, 50];
a.pop();
console.log(a);
//output:[20,30,40]
JavaScript Array.shift() Method:
The shift() method is used to remove elements from the beginning of an array
let a = [20, 30, 40, 50];
a.shift();
console.log(a);
//output:[30,40,50]
JavaScript Array.splice() Method:
The splice() method is used to Insert and Remove elements in between the Array.
let a = [20, 30, 40, 50];
a.splice(1, 3);
a.splice(1, 0, 3, 4, 5);
console.log(a);
//output:[20,3,4,5]
Explanation:
The first a.splice(1, 3) removes 3 elements (30, 40, 50) starting at index 1. The array becomes [20].
The second a.splice(1, 0, 3, 4, 5) inserts 3, 4, and 5 at index 1 without removing anything. The array becomes [20, 3, 4, 5].
JavaScript Array.slice() Method:
The slice() method returns a new array containing a portion of the original array, based on the start and end index provided as arguments.
const a = [1, 2, 3, 4, 5];
const res = a.slice(1, 4);
console.log(res);
console.log(a)
//output:
//[2,3,4]
//[1,2,3,4,5]
Explanation:
The slice() method creates a new array by extracting elements from index 1 to 3 (exclusive of 4) from the original array.
The original array remains unchanged, and the result is [2, 3, 4].
JavaScript Array some() Method:
The some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument function.
const a = [1, 2, 3, 4, 5];
let res = a.some((val) => val > 4);
console.log(res);
//output:true
JavaScript Array every() Method:
The every() method checks every elements of the array satisfies the condition checked by the argument function.
const a = [1, 2, 3, 4, 5];
let res = a.every((val) => val > 4);
console.log(res);
//output:false
JavaScript Array map() Method:
The map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method.
let a = [4, 9, 16, 25];
let mul = a.map((x)=>x*2);
console.log(mul);
//output:[8,18,32,50]
** JavaScript Array filter() method:**
The filter() method in JavaScript creates a new array with all elements that pass the test implemented by the provided function. It does not modify the original array.
let a1 = [1, 2, 3, 4, 5]
let a2 = a1.filter((num) => num > 1)
console.log(a2);
console.log(a1);
//output:[2,3,4,5]
//[1, 2, 3, 4, 5]
JavaScript Array reduce() Method:
The reduce() method is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
let a = [88, 50, 25, 10];
let sub = a.reduce(geeks);
function geeks(total, num) {
return tot - num;
}
console.log(sub);
//output:3
Explanation:which subtracts each element (num) from the running total (total)-is consider the first element.
In real time using for calculate
*Student marks total
*employee salary total
JavaScript Array reverse() method:
The reverse() method is used to reverse the order of elements in an array. It modifies the array in place and returns a reference to the same array with the reversed order.
let a = [1, 2, 3, 4, 5];
a.reverse();
console.log(a);
//output:[5,4,3,2,1]
JavaScript Array values() method
The values() method returns a new Array Iterator object that contains the values for each index in the array.
const a = ["Apple", "Banana", "Cherry"];
const res = a.values();
for (const value of res) {
console.log(value);
}
//output:Apple
//Banana
//Cherry
- The values() method returns an iterator object that allows iterating over the values of the 'a' array.
- The for...of loop is used to iterate over the iterator and log each fruit ("Apple", "Banana", "Cherry") to the console.
Top comments (0)