DEV Community

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

Posted on

2

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]

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay