Javascript Arrays for Absolute Beginner

Array Built-in Methods in Javascript

Array in javascript have lots of built-in methods that makes the concept productive enough and fun to use. let check out the built-in array methods in javascript.

join() and toString() methods are used to convert our arrays to a string.

Array.Join(“ , ”)  //Strawberry-Cookies , Stick-Sweets , Lollipop , Kolas , Vanilla

To add and remove array elements in javascript we use the push and pop method respectively. In javascript arrays the structured at which pop() and push() method is called LIFO , last in, first out.

 Let Cookies = [“Strawberry-Cookies”, “Stick-Sweets”, “Lollipop”, “Kolas”, “Pinky”]

But, in order to change that structure, and remove elements from the first index we make use of the shift() method. Shifting and popping works the same way only that, shift works on the first element instead of the last.
Also with unshift() method we push elements to the first index of the array.

splice() method can be used to add new element in the array and if perfectly formatted, splice method can be used to remove elements from an array.

Splice (Start, DelCount, Opt)

start: defines the position where new elements should be added (spliced in).
delCount: defines how many elements should be removed
opt: optionally, defines the elements to be added

With concat() method we can merge two arrays together to generate a new separate array.

 Let Minerals = [“Coca-Cola”, “Sprites”];
 Let Lemon = [“Lemonade”];
 Let SoftDrinks = Minerals.Concat(Lemon); 

The slice() method slices out a piece of an array into a new array.

 Let Colors = [“Blue”, “Orange”, “Green”];
 Let SlicedColor = Colors.Slice(1);

The sort() method is used to sort the elements in the arrays

Leave a Comment

Your email address will not be published. Required fields are marked *