DEV Community

Cover image for Basic Array Methods In JavaScript.
Dev Raj Sharma
Dev Raj Sharma

Posted on

Basic Array Methods In JavaScript.

Lets Create An Array of Integers
let numbers = [5, 1, 7, 3];
console.log(numbers);

[ 5, 1, 7, 3 ]

1. Finding the length of the Array
let lengthOfArray = numbers.length;
console.log(lengthOfArray);

4

2. Sortng the array in ascending order
numbers.sort();
console.log(numbers);

[ 1, 3, 5, 7 ]

3. Adding new element in last of the index
numbers.push(6);
console.log(numbers)
;

[ 1, 3, 5, 7, 6 ]

4. Adding new Element in starting of the array
numbers.unshift(8);
console.log(numbers);

[ 8, 1, 3, 5, 7, 6 ]

5. Removing or Deeleting a first element of the array
numbers.shift();
console.log(numbers);

[ 1, 3, 5, 7, 6 ]

6. Removing or Deleting a last element of the array
numbers.pop();
console.log(numbers);

[ 1, 3, 5, 7 ]

7. Finding element of the targeted index

let index = 2;
console.log(At index of ${index} element is ${numbers.at(index)});

At index of 2 element is 5

8. Merging two or more array
const array1 = [1,2,3,4,5];
const array2 = [6,7,8,9];
const newArray = array1.concat(array2);
console.log(newArray)

[1, 2, 3, 4, 5 ,6, 7, 8, 9]

Top comments (0)