Adding elements to an array in JavaScript

JavaScript arrays are objects which can be initialized using a syntax of
["", ...] Parentheses with a number of elements ie
var oddNumbers = [2, 4, 6]
or var numbers = ["one", "two", "three"]

Or the global Array constructor using syntax ie 
var integer = new Array(length = 2)
console.log(integer) // [null, null]

where length species blank filler value by default the element is null and you can't iterate them to change the fill value you can call the fill method of an array ie
console.log(integer) // [0, 0]

You can append an element to any of the arrays declared above using push , prepend using unshift, assign using manual assigning

Manual assignment
Simplest way is by simply assigning a new element
oddNumbers['index'] = "Number"
ie oddNumbers[3] = 8;
console.log(oddNumbers) // [2, 4, 6, 8]

Its a method for appending elements to an array is
console.log(oddNumbers) // [0, 2, 4, 6, 8]

Its a method for prepending elements to an array
console.log(oddNumbers) // [0, 2, 4, 6, 8, 10]


Popular posts from this blog

How to set content disposition header for nginx server to force content download

Animated daylight on HTML5 Canvas

How to make a static http server in nodejs using express

JS13K Preparation

Adding Gameover and introduction screen on HTML Canvas