59 lines
1.9 KiB
JavaScript
59 lines
1.9 KiB
JavaScript
// METHODS OF ARRAYS
|
|
/*
|
|
push(elem_list): Adds one or more elements to the end of the array,
|
|
and returns the new length of the array.
|
|
|
|
pop(): removes the last element in the array, decrements the length,
|
|
and the element that it removed
|
|
|
|
unshift(elem_list): adds one or more elements to the beginning of the
|
|
array and returns the new length of the array
|
|
|
|
shift(): Removes the first element in the array, decrements the
|
|
array length, and returns the element that it removed
|
|
|
|
join(seperator): when no parameter is passed, this method converts all the elements
|
|
of the array to strings and concatenates them seperated
|
|
by commas. To change the seperator, you can pass this method
|
|
a string literal
|
|
|
|
toString(): Same as the join method without any parameter passed to it.
|
|
*/
|
|
|
|
// creating an array of strings
|
|
console.log("Original Array")
|
|
let animals = ["Zebra", "Koala", "Lion", "Elephan", "Eagle"]
|
|
console.log(animals)
|
|
console.log()
|
|
|
|
console.log("Inserting Monkey and Snake at end of array")
|
|
animals.push("Monkey", "Snake");
|
|
console.log(animals)
|
|
console.log()
|
|
|
|
console.log("Removing last element of array")
|
|
animals.pop()
|
|
console.log(animals)
|
|
console.log()
|
|
|
|
console.log("Inserting Spider and horse at front of array")
|
|
animals.unshift("Spider", "horse")
|
|
console.log(animals)
|
|
console.log()
|
|
|
|
console.log("Removing first item from array")
|
|
animals.shift()
|
|
console.log(animals)
|
|
console.log()
|
|
|
|
console.log("Joining array elements in a single string")
|
|
let s = animals.join(" ")
|
|
console.log(s)
|
|
console.log()
|
|
|
|
// combining array seaanimals and animals into one array named allanimals
|
|
console.log("combining array seaanimals and animals into one array named allanimals")
|
|
let seaAnimals = ["Shark", "Whale", "Dolphin"]
|
|
let allanimals = seaAnimals.concat(animals)
|
|
console.log(allanimals)
|
|
console.log() |