0

i'm trying to push a string elements to sized array but when pushing an element the sized array start insert the elements in the last index of the array and grow but i want to insert the first element in the index [0] and second on [1] and so on till index [3] but i don't know why it start to insert elements after the last index and grow when add more element my code :

var users = new Array(3);
users.push(someData);
console.log(users);
console.log(users.length)

and the result like this:

[ , , , 'd', 'dd' ]
5

6 Answers 6

3

Avoid using new Array(count). Just create an empty array and then push to it like that:

var users = [];
users.push(someData);

Or, if you have some initial static content, this will be better:

var users = [ someData ];

The reason why I advise you to avoid new Array(...) is because it can be confusing and error-prone:

  • new Array(3) will create an array with 3 items: [undefined, undefined, undefined].
  • new Array(3, 4) will create an array containing the items 3 and 4: [3, 4].
Sign up to request clarification or add additional context in comments.

Comments

2

Push adds a new element to the end of the array. You create the array with three empty entries then push somethign onto the end. Javascript does not have limited sized arrays. In your case I would guess you want to create an empty arraya nd just push onto it.

Comments

1

The argument you pass to the new Array call sets the initial size, not the maximum size. There is no max size. Just create the array like this:

var users = new Array()

or

var users = []

Comments

1

When you create an array with the constructor and specify a size, that will only set the current length of the array, it won't limit the size of the array.

Doing this:

var users = new Array(3);

does exactly the same thing as this:

var users = new Array();
users.length = 3;

Growing the array by setting the length doesn't actually put anything in the array, it will only set the length property.

The push method looks at the length property when it adds an item, so doing this:

users.push(someData);

does exactly the same thing as:

users[users.length] = someData;

If you want your array to be empty from the start, just create it without setting the length:

var users = new Array();

You can also use the array literal syntax to create an empty array:

var users = [];

Arrays doesn't support limiting the size, so if you want a collection with a limited size, you have to create it yourself (or find one that someone else has created).

1 Comment

i did that when the i know from the other answers that Javascript does not have limited sized arrays but thank u for your answer to
0

array.push() method of array insert element from last index of array. use array.unshift() method to push from starting index of array

3 Comments

This will keep 3 undefined values at the end of the array, not replacing them.
i try array.unshift() and yes it begin to add elements from the start index but the same as when array.push() the array size still growing when adding new elements and i just need the array to continent only 4 elements
javascript don't have fixed size array.so you can't.
0

Because in javascript "var users = new Array(3);" means creation of array with 3 undefined elements.

For basic Array initialization details you can refer to : http://www.w3schools.com/js/js_arrays.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.