Add new item to an array CODE
Creates
myFavoriteNumbers
array
let myFavoriteNumbers = [5, 14, 16, 18]
Adds new item
22
to the array
myFavoriteNumbers.push(22)
Adds the array as a new item to the
myFavoriteNumbers
array
myFavoriteNumbers.push([23, 24])
Adds the object to the array as new item
myFavoriteNumbers.push({firstNumber: 25, secondNumber: 26})
Adds the function to the array as new item
myFavoriteNumbers.push(function(){console.log('I am fired!')})
Adds new string item to the array
myFavoriteNumbers.push('27')
Let's fire the function that we added as an item
myFavoriteNumbers[7]()
After running the function the result will be:
I am fired!
by Valeri Tandilashvili
4 years ago
JavaScript
Arrays
1
Pro tip: use ```triple backticks around text``` to write in code fences