Array
Arrays can be used to store one or more values in a variable.
Syntax
Ways to store values in an array
- Initialise the array with all the values
- Initialise array using index
What if we don't add value to the particular index what will be the output? Let's check this with example.
- Add values to array using push()
Access values in array
Values can be accessed from an array using:
- index
- pop() - removes last element from the array.
- shift() - removes first element from the array.
Note: pop() and shift() removes element from the array and returns the removed element. If you don't want to change the array, access array using index.
How can we get,add or remove multiple array of elements all at once from the initialized array. For this we can use:
- slice()
- splice()
Lets see how can we use both these functions.
- slice(start,end) - takes two parameters start and end.
- start defines the starting index in the array.
- end defines the last index in the array.
- returns elements from the array between the start and the end index.
- does not change the orignal array.
Elements in between the starting and ending index will be included.
- splice(start,deleteCount, element1, element2, element3,..,upto N elments)
- start defines the starting index.
- deleteCount states the number of elements you want to delete including start.
- elementN are the ones that you want to replace with the deleted elements.
- returns elements from the array between the start and the end index.
- changes the orignal array.
Objects
Object stores multiple values in key value pairs.
new key,values can be added and removed from the object as well.
That's all.
BYEE.👋