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.
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.
Array Methods
Push and Pop
Push - To push element in the array
Pop - To pop element from the array
Shift - Removes the first element unlike Pop which removes the last element.
Unshift - Adds an element in the front of array.
Modifying Existing Element
Concatenating two elements
Splicing - used to add new element(s) in the array
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
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.