JSSSSSSSSSSSSSSSSSSSSSSS
JSSSSSSSSSSSSSSSSSSSSSSS
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)