JavaScript Basic Array Methods

JavaScript Basic Array Methods

In this article we will learn about some basic array methods and their uses.

slice()

The slice() method Will return new array but only with the extracted parts

For example:

let arr = ['a', 'b', 'c', 'd', 'e'];

arr.slice(2) // ['c'.'d','e']

arr.slice(2, 4) // ["c", "d"]

arr.slice(-2) // ["d", "e"]

arr.slice(1, -2) // ["b", "c"]   

splice()

The splice() method works the same as slice but difference is it does actually change the original array and Extracted elemented will gone from origianl array.

Basically we use it to just delete one or more eleemnet frpm an array using splice

arr.splice(2);
arr // ["a", "b"]

Remove last element using splice() method:

arr.splice(-1)
arr//["a","b","c","d","e"]

Reverse()

The reverse method is used to reverse array element.

const arr2 = ['j', 'i', 'h', 'g', 'f'];

console.log(arr2.reverse());
console.log(arr2);

concat()

const letters = arr.concat(arr2);
console.log(letters);
console.log([...arr, ...arr2]);

join()

console.log(letters.join(' - '));

Conclusion:

Do let me know If you face any difficulties please feel free to comment below we love to help you.

If you have any feedback suggestion then please inform us by commenting.

Don’t forget to share this tutorial with your friends on Facebook and Twitter

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment