0% found this document useful (0 votes)
7 views1 page

JSSSSSSSSSSSSSSSSSSSSSSS

JavaScript arrays are special variables that can hold multiple values, created using array literals. They can be manipulated using various methods such as pop, push, shift, unshift, concat, splice, and slice. Arrays use numbered indexes, while objects use named indexes, making arrays suitable for numerical indexing and objects for string indexing.

Uploaded by

dolapodavid127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

JSSSSSSSSSSSSSSSSSSSSSSS

JavaScript arrays are special variables that can hold multiple values, created using array literals. They can be manipulated using various methods such as pop, push, shift, unshift, concat, splice, and slice. Arrays use numbered indexes, while objects use named indexes, making arrays suitable for numerical indexing and objects for string indexing.

Uploaded by

dolapodavid127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Javascript Arrays

An array is a special variable that can hold more than one value.

Examples 0 1 2
const fruitNames=["apple", "mango", "banana"]
fruitNames.length=3
fruitNames[0]
fruitNames[fruitNames.length-1]
fruitNames[2]
fruitNames

Creating an array
using an array literal is the easiest way to create an array in javascript e.g
const array_name=[item,item]
for best practices, declare your array names with "const"
the index of an array starts with 0.
the length of an array the total number of items in the array starts counting from
number 1
arrays have data types of objects.
accessing the first item of the array is like this
fruitNames[0]
while accessing the last array element is like this
fruitNames[fruitNames.length-1]
one way to look through the elements of an array is to use for loop

Array methods(functions)
(pop)- this method removes the last item of an array. e.g fruitNames.pop()
(push)- this method adds an element in from the back. e.g fruitNames.push('orange')
(shift)- this method takes the first element of an array. e.g fruitNames.shift()
(unshift)- this method adds an array to the beginning of an array. e.g
fruitNames.unshift('pear')
(concat)- this method creates a new array by merging a new array together. e.g
const myArr = ['item 1' , 'item 2']
const newArray = studentNames.concat(myArr)
(splice)- this method adds new items to an array or removes an item from an array
e.g //Eg; splice(position/index, number of items to remove, item to add)
(slice)- this method slices out a piece of an array into a new array. e.g //Eg;
slice(start position/index, end position/index);
studentNames = studentNames.slice(1, 3)

difference between objects and arrays


Arrays use numbered indexes
Javascript objects use named indexes

When to use arrays or objects


1. you should use objects when you want the element indexes to be strings.
2. you should use arrays when you want the element to be numbers.

You might also like