Black Friday Sale Upgrade Your Home →

JavaScript Array

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

pic

  1. Array Literal
  2. new Keyword

Array Literal

JS
const dailyActivities = ['eat', 'code', 'sleep']
'
**new keyword**
you can also create an Array Using JavaScript's new Keyword
'Example
const 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 element
console.log(letter[0]) // "a"
console.log(letter(2)) // "m"

Array Index Sart With 0.

Add An element to an Array

pic

There are build in methods to add elements to an array

  1. push()
  2. 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

pic

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

pic

you can also find the length of an array by usin length property

JS
let dailyActivities= ['eat', 'code', 'sleep'];
console.log(dailyActivities.length);

  Previous      Next