let arr = [1,2,3,4]
console.log(arr) // [1,2,3,4]
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
let arr = [1,2,3,4]
- Initialise array using index
let arr = []
arr[0] = 'Audi'
arr[1] = 'BMW'
arr[2] = 'Ferrari'
What if we don't add value to the particular index what will be the output? Let's check this with example.
let arr = []
arr[0] = 'Audi'
arr[2] = 'Ferrari'
console.log(arr) // ['Audi', , 'Ferrari']
console.log(arr[1]) // undefined
- Add values to array using push()
let arr = []
arr.push('Cricket')
arr.push('Football')
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.
let arr = ['Audi','BMW','Ferrari','Porsche']
console.log(arr[1]) // BMW
console.log(arr.pop()) // Porsche
console.log(arr.shift()) // Audi
console.log(arr) // ['BMW', 'Ferrari']
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.- **Element at the starting index is included**. - **Element at the ending index is not included**.
//INDEX --> 0 1 2 3
let arr = ['Audi','BMW','Ferrari','Porsche']
let copyArr = arr.slice(1,3)
console.log(copyArr) // [ 'BMW', 'Ferrari' ]
- 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.
let arr = ['Audi','BMW','Ferrari','Porsche']
let afterSplice = arr.splice(1,2)
console.log(afterSplice) // [ 'BMW', 'Ferrari' ]
console.log(arr) // [ 'Audi', 'Porsche' ]
// 0 states no element to delete but instead add element after the given index
arr.splice(1,0,'BMW')
console.log(arr) // [ 'Audi', 'BMW', 'Porsche' ]
Objects
Object stores multiple values in key value pairs.
let obj = { name: 'tom', age: 5}
// reassign values to the obj
obj.name = 'jerry'
console.log(obj) // { name: 'jerry', age: 5}
new key,values can be added and removed from the object as well.
let obj = { name: 'tom', age: 5}
// adding new key value to object
obj.country = 'INDIA'
obj['state'] = 'PUNJAB'
console.log(obj) // { name: 'tom', age: 5, country: 'INDIA', state: 'PUNJAB' }
// removing key will delete all the values it holds
delete obj.state
console.log(obj) { name: 'tom', age: 5, country: 'INDIA' }
That's all.
BYEE.👋