Introduction
Array It is an Object where we can store multiple Valuesat once
JS
const dailyActivities = ['eat', 'code', 'sleep']
Two Types to Create An Array
- Array Literal
- new Keyword
Array Literal
JS
const dailyActivities = ['eat', 'code', 'sleep']'**new keyword**you can also create an Array Using JavaScript's new Keyword'Exampleconst dailyActivities = new Array['eat', 'code', 'sleep']
Access Element Of An Array
pic you can access elements of an array using indices 0,1,2....
JS
const letter= ['a', 'i', 'm', 'a', ']//first elementconsole.log(letter[0]) // "a"console.log(letter(2)) // "m"
Array Index Sart With 0.
Add An element to an Array
There are build in methods to add elements to an array
- push()
- unshift()
push()
This method is used to add an element at end of the array
JS
let dailyActivities= ['eat', 'code']dailyActivities.push('sleep');console.log(dailyActivities)
unshift()
This method is used to add an element at beginneig of the array
JS
let dailyActivities= ['eat', 'code']dailyActivities.unshift('sleep');console.log(dailyActivities)
Change element of an Array
you can aslo add or change the elements by accessing the index value.
JS
let dailyActivities= ['eat', 'code']dailyActivities[1] = 'sleep'console.log(dailyActivities)
remove an elment of an array
pic you can aslo remove the last elements of array by using pop() method.
JS
let dailyActivities= ['eat', 'code', 'sleep']dailyActivities.pop();console.log(dailyActivities)
Array Length
you can also find the length of an array by usin length property
JS
let dailyActivities= ['eat', 'code', 'sleep'];console.log(dailyActivities.length);