DEV Community

Osama
Osama

Posted on

Javascript 1O1 series: Array’s Methods And Iterators Part(1), Methods.

javascript


You'll learn:

Some useful array methods to make dealing with arrays is an easy thing, I hope you won't find any issues with handling arrays operations from now on.


Insert and Remove:

shift:
shift() is used to remove the first element of the array.
Array.shift()
shift

unshift:
unshift is used to insert an element into the array, unshift will add the element to the start of the array
unshift

push:
push is used to append elements to the array, (add to the end)
push

pop:
pop is used to pop out (remove) the last element of the array
pop

Sub-arrays:

slice:
slice is used to copy elements in a specific range.
e.g.you have an array and you want to get the elements between index 1 and index 3
slice

when the end parameter is not defined, then the slice extends to the end of the array

let slicedArr2 = arr.slice(1)
// [2,3,4,5,6,7,8,9]

spliced:
slice is used to REMOVE elements from an array.
spliced

slice and splice meme


Reordering:

reverse:
used to reverse the array's elements.
reverse

sort:

By default, the sort() function sorts values as strings.
This works well for strings ("Apple" comes before "Banana").
However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce incorrect result when sorting numbers.
You can fix this by providing a compare function.

sort

so without compare function, sort() is not that useful.
more about sort:

Array.prototype.sort() - JavaScript | MDN

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

favicon developer.mozilla.org

Search and Sort:

includes:
used to check if an element does exist or not.
includes

at:
simple, you pass an index and you get the value in that index.
at

indexOf:
indexOf() is the opposite of at(), you pass a value and get the value's index in the array.
indexOf

sort:
sort can be used here as well

By default, the sort() function sorts values as strings.
This works well for strings ("Apple" comes before "Banana").
However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce incorrect result when sorting numbers.
You can fix this by providing a compare function.

sort

so without compare function, sort() is not that useful.
more about sort:

Array.prototype.sort() - JavaScript | MDN

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

favicon developer.mozilla.org

Adding and combining arrays :

concat:
used to combine (add together) two or more arrays, non of the combined arrays will be modified, concat() will return a new array instead.
concat


Array to String conversion:

toString:
used to represent an array in string format.

toString

there is also toLocalString(), search about it as an exercise and learn the difference between toString() and toLocalString(), if you know, please share the answer in a comment.


Other methods:

flat:
when you have a nested arrays and want to pull out all these nested arrays to a new array, flat() is used.
flat() doesn't modify the array, it returns a new array instead.
flat

fill:
used to fill the whole or part of the array with a specific value.
fill


References:


JavaScript: The Definitive Guide, 7th Edition [Book]

JavaScript is the programming language of the web and is used by more software developers today than any other programming language. For nearly 25 years this best seller has been … - Selection from JavaScript: The Definitive Guide, 7th Edition [Book]

favicon oreilly.com


Array - JavaScript | MDN

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

favicon developer.mozilla.org

JavaScript Basic Array Methods - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

favicon geeksforgeeks.org

15 Must-Know JavaScript Array Methods

Master JavaScript arrays with these 15 methods and become a more productive JavaScript developer.

favicon livecodestream.dev

Thanks for reading, and feel free to ask any question about javascript or this series, and I appreciate any feedback to improve My content.

Find me on twitter, github and my portfolio.

Top comments (0)