JavaScript provides many built-in methods to make working with arrays easier. These methods help you add, remove, find, and modify elements quickly.
Table of Contents
- JavaScript Array length
- JavaScript Array toString() Method
- JavaScript Array join() Method
- JavaScript Array delete Operator
- JavaScript Array concat() Method
- JavaScript Array flat() Method
- JavaScript Array push() Method
- JavaScript Array unshift() Method
- JavaScript Array pop() Method
- JavaScript Array shift() Method
- JavaScript Array splice() Method
- JavaScript Array slice() Method
- JavaScript Array some() Method
- JavaScript Array map() Method
- JavaScript Array filter() Method
- JavaScript Array reduce() Method
- JavaScript Array reverse() Method
- JavaScript Array values() Method
JavaScript Array length
In JavaScript, the length property tells you how many elements are in an array. It updates automatically when you add or remove elements.
let a = ["HTML", "CSS", "JS", "React"];
console.log(a.length); // Output: 4JavaScript Array toString() Method
The toString() method converts the given array into a string with each element separated by commas.
let a = ["HTML", "CSS", "JS", "React"];
let s = a.toString();
console.log(s); // Output: HTML,CSS,JS,ReactJavaScript Array join() Method
The join() method creates and returns a new string by concatenating all elements of an array using a specified separator.
let a = ["HTML", "CSS", "JS", "React"];
console.log(a.join('|')); // Output: HTML|CSS|JS|ReactJavaScript Array delete Operator
The delete operator removes a specific property from an object or element from an array.
let emp = { firstName: "Riya", lastName: "Kaur", salary: 40000 };
console.log(delete emp.salary); // Output: true
console.log(emp); // Output: { firstName: 'Riya', lastName: 'Kaur' }JavaScript Array concat() Method
The concat() method is used to merge two or more arrays.
let a1 = [11, 12, 13];
let a2 = [14, 15, 16];
let a3 = [17, 18, 19];
let newArr = a1.concat(a2, a3);
console.log(newArr);JavaScript Array flat() Method
The flat() method flattens a nested array structure into a single array.
const a1 = [['1', '2'], ['3', '4', '5',['6'], '7']];
const a2 = a1.flat(Infinity);
console.log(a2); JavaScript Array push() Method
The push() method adds elements to the end of an array.
let a = [10, 20, 30, 40, 50];
a.push(60, 70, 80, 90);
console.log(a);JavaScript Array unshift() Method
The unshift() method adds elements to the beginning of an array.
let a = [20, 30, 40];
a.unshift(10, 20);
console.log(a);JavaScript Array pop() Method
The pop() method removes the last element from an array.
let a = [20, 30, 40, 50];
a.pop();
console.log(a);JavaScript Array shift() Method
The shift() method removes the first element from an array.
let a = [20, 30, 40, 50];
a.shift();
console.log(a);JavaScript Array splice() Method
The splice() method is used to add or remove elements from an array.
let a = [20, 30, 40, 50];
a.splice(1, 3);
a.splice(1, 0, 3, 4, 5);
console.log(a);JavaScript Array slice() Method
The slice() method extracts elements from an array without modifying it.
const a = [1, 2, 3, 4, 5];
const res = a.slice(1, 4);
console.log(res);JavaScript Array some() Method
The some() method tests if at least one element in an array satisfies a condition.
const a = [1, 2, 3, 4, 5];
let res = a.some(val => val > 4);
console.log(res);JavaScript Array map() Method
The map() method creates a new array by applying a function to each element.
let a = [4, 9, 16, 25];
let sub = a.map(Math.sqrt);
console.log(sub);JavaScript Array filter() Method
The filter() method creates a new array containing elements that pass a test.
let a1 = [1, 2, 3, 4, 5]
let a2 = a1.filter(num => num > 1)
console.log(a2)JavaScript Array reduce() Method
The reduce() method applies a function to an array to reduce it to a single value.
let a = [88, 50, 25, 10];
let sub = a.reduce((tot, num) => tot - num);
console.log(sub);JavaScript Array reverse() Method
The reverse() method reverses the order of elements in an array.
let a = [1, 2, 3, 4, 5];
a.reverse();
console.log(a);JavaScript Array values() Method
The values() method returns an iterator object for the array.
const a = ["Apple", "Banana", "Cherry"];
const res = a.values();
for (const value of res) {
console.log(value);
}