Arrays Methods - Javascript

What are Arrays in JS?

Arrays are special type of objects which uses numbers or index to access elements inside it.

In this example typeof array gives us object.

js-1.png

In javascript arrays can contain different data types in the same container for example This array contains values of type integer, string and boolean in the same container.

js-2.png

Array Methods

Push and Pop

Push - To push element in the array

js-3.png

Pop - To pop element from the array

js-4.png

Shift - Removes the first element unlike Pop which removes the last element.

js-5.png

Unshift - Adds an element in the front of array.

js-6.png

Modifying Existing Element

js-10.png

Concatenating two elements

js-11.png

Splicing - used to add new element(s) in the array

js-7.png

In the first example we did arr.splice(1,0,'Ram') here

  • 1 - is the position where you want to start adding elements.
  • 0 - number of elements you want to remove.
  • 'Ram' - value you want to add

Slicing - used to slice elements from the array without changing the original array

js-9.png

In the first example it simply sliced element present at index 1

In the second example it sliced element starting from 0 to 1 Text point to be noted 2 was exclusive it wasn't included because it the event of 2 parameters it'll start from starting position and end at (2nd argument - 1)th position.