-5

Appending to array

How do I append to an array in Javascript

Andrei
  • 3,086
  • 2
  • 19
  • 24
oops
  • 205
  • 2
  • 11

2 Answers2

2

Make a new array.

var fruits = ["Banana", "Orange", "Apple", "Mango"];

And then push values like this

fruits.push("Kiwi");
aandis
  • 4,084
  • 4
  • 29
  • 40
1

You can do this:

// create a new array, using the `literals` instead of constructor
var array = [];

// add a value to the last position + 1 (that happens to be array.length) 
array[array.length] = 10;

or

 // use the push method from Array.
 array.push(10);

Also, if you have an Object and you want it to behave like an array (not recommended), you can use

Array.prototype.push.call(objectLikeArray, 10);
Andrei
  • 3,086
  • 2
  • 19
  • 24