0% found this document useful (0 votes)
59 views

JS4 ClassNotes

Arrays in JavaScript allow us to store and manipulate collections of data. We can create arrays using square bracket notation and assign them to variables. Arrays have numeric indices that start at 0 to access individual elements. We can loop through arrays to access each element. Common array methods include push() to add to the end, pop() to remove from the end, unshift() to add to the start, and shift() to remove from the start. The splice() method allows changing the original array by adding, removing, or replacing elements.

Uploaded by

aniketpathak0002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

JS4 ClassNotes

Arrays in JavaScript allow us to store and manipulate collections of data. We can create arrays using square bracket notation and assign them to variables. Arrays have numeric indices that start at 0 to access individual elements. We can loop through arrays to access each element. Common array methods include push() to add to the end, pop() to remove from the end, unshift() to add to the start, and shift() to remove from the start. The splice() method allows changing the original array by adding, removing, or replacing elements.

Uploaded by

aniketpathak0002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Arrays in JS

Collections of items

Create Array

lege
Col
a
let heroes = [ “ironman”, “hulk”, “thor”, “batman” ];

Apn
let marks = [ 96, 75, 48, 83, 66 ];

let info = [ “rahul”, 86, “Delhi” ];


Arrays in JS
Array Indices

arr[0], arr[1], arr[2] ....

lege
Col
pna
0 1 2
A
3 4
Looping over an Array
Print all elements of an array

lege
Col
pna
A
Let‘s Practice
Qs. For a given array with marks of students -> [85, 97, 44, 37, 76, 60]

e
Find the average marks of the entire class.

lleg
a Co
Apn
Let‘s Practice
Qs. For a given array with prices of 5 items -> [250, 645, 300, 900, 50]

e
All items have an offer of 10% OFF on them. Change the array to store final price after

leg
applying offer.

Col
pna
A
Arrays in JS
Array Methods

lege
l
Push( ) : add to end

a Co
Apn
Pop( ) : delete from end & return

toString( ) : converts array to string


Arrays in JS
Array Methods

lege
l
Concat( ) : joins multiple arrays & returns result

a Co
Unshift( ) : add to start

Apn
shift( ) : delete from start & return
Arrays in JS
Array Methods

lege
ol
Slice( ) : returns a piece of the array

na C
slice( startIdx, endIdx )

Ap
Splice( ) : change original array (add, remove, replace)

splice( startIdx, delCount, newEl1... )


Let‘s Practice
Qs. Create an array to store companies -> “Bloomberg”, “Microsoft”, “Uber”, “Google”, “IBM”, “Netflix”

lege
ol
a. Remove the first company from the array

na C
Ap
b. Remove Uber & Add Ola in its place

c. Add Amazon at the end

You might also like