Javascript
JS Arrays
Arrays Methods

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

  1. JavaScript Array length
  2. JavaScript Array toString() Method
  3. JavaScript Array join() Method
  4. JavaScript Array delete Operator
  5. JavaScript Array concat() Method
  6. JavaScript Array flat() Method
  7. JavaScript Array push() Method
  8. JavaScript Array unshift() Method
  9. JavaScript Array pop() Method
  10. JavaScript Array shift() Method
  11. JavaScript Array splice() Method
  12. JavaScript Array slice() Method
  13. JavaScript Array some() Method
  14. JavaScript Array map() Method
  15. JavaScript Array filter() Method
  16. JavaScript Array reduce() Method
  17. JavaScript Array reverse() Method
  18. 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: 4

JavaScript 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,React

JavaScript 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|React

JavaScript 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);
}