Reputation: 7427
In JavaScript, if you set an array to be of size 5 ( var foo = new Array(5);
), is this just an initial size? Can you expand the number of elements after it is created. Is it possible to do something like this as well - arr = new Array()
and then just assign elements one by one? Thanks in advance :-)
Upvotes: 5
Views: 628
Reputation: 817228
Arrays are dynamic in JavaScript. You don't have to initialize them with a certain length. In fact you should use the literal notation []
because of the Array
constructor's ambiguity:
If you pass only one parameter to Array
, it will set the array length to this parameter. If you pass more than one parameter, these elements are added to the array.
How is the size of the array determined?
The size of an array is the highest index + 1. This can be quite confusing. Consider this:
var arr = [];
arr[41] = 'The answer?';
console.log(arr); // [undefined, undefined, ..., 'The answer?']
console.log(arr.length) // 42
You can even set the length yourself by assigning a number to .length
:
arr.length = 99;
If you now add a new element using arr.push()
, it will get the index 100
and the length will increase. Whenever you add an element to the array via an index, it is tested whether arr.length
is smaller than the index and updated accordingly. But it does not get smaller.
So in fact what var arr = new Array(5)
is doing is setting the length of the array to 5
. Nothing else.
For more information about creating and populating arrays, I suggest to read about it in the MDC JavaScript Guide.
Upvotes: 0
Reputation: 50612
When you give a new array an explicit size in javascript (using new Array(5)
), you populate each index with value of undefined
. It is generally considered better practice to instantiate using the array literal []
expression:
var arr = [];
Then, you can push new elements using push()
var arr = [];
arr.push('first value'):
arr.push('second value'):
Check out the MDC Array documentation for more info: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
Upvotes: 5
Reputation: 7249
Theres a few ways to declare and put values in an array.
First like what you want to do,
var myarr = new Array();
myarr[0] = 'element1';
myarr[1] = 'element2';
myarr[2] = 'element3';
Second way is to define them
var myarr =new Array("element1","element2","element3");
and third is similar to the second
var myarr =["element1","element2","element3"];
You can also check out https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array for a little more information about using the arrays as well. You could use push and pop if you wanted to as well.
If you use jquery or mootools they also have built-in functions to perform on arrays, http://api.jquery.com/jQuery.each/ for instance.
Upvotes: 2
Reputation: 73041
Check the documentation for Array, but the simple answer to your question is yes.
var arr5 = new Array(1, 2, 3, 4, 5); // an array with initial 5 elements
var arr = new Array(); // an array without initial
You can also use array literals:
var arr5 = [1, 2, 3, 4, 5];
var arr = [];
Upvotes: 1
Reputation: 14375
Yes it is just an initial size, and it is not required. If you don't use a single number, you can immediately populate.
It is also more common to use the simpler []
syntax.
var arr = ['something', 34, 'hello'];
You can set (or replace) a specific index by using brackets:
arr[0] = "I'm here replacing whatever your first item was";
You can add to the end by using push:
arr.push('add me');
There may be faster performance in some browsers if you do it like this instead:
arr[arr.length] = 'add me';
You can add something at any index.
You can remove an item completely using splice:
arr.splice(0, 1); // remove first item in the array (start at index 0, with 1 being removed)
Upvotes: 5
Reputation: 2854
Have a look at http://www.w3schools.com/js/js_obj_array.asp
var myCars=new Array(); // regular array (add an optional integer
myCars[0]="Saab"; // argument to control array's size)
myCars[1]="Volvo";
myCars[2]="BMW";
Upvotes: 1