0

I am trying to store values in array using javascript..but i get strange error in javascript.below is my code

var a = 1;
for(i=0;i<4;i++)
{

var all = new Array();
all[i]=a;
a++;
}

alert(all[1]);
alert(all[2]);
alert(all[3]);

please check the code here : http://jsfiddle.net/D8Suq/

for all[1] and all[2] i am getting undefined error..but all[3] is working fine,,,am confused.some one please help me

vishnu Simbu
  • 43
  • 1
  • 4
  • 10

9 Answers9

10

You are reassigning your array in every loop iteration (which deletes everything in it) instead of only before the whole loop.

This should work as expected:

var a = 1;
var all = new Array();
for(i=0;i<4;i++)
{
    all[i]=a;
    a++;
}

alert(all[1]);
alert(all[2]);
alert(all[3]);
s1lence
  • 2,188
  • 2
  • 16
  • 34
2

You are re-initializing the array inside of the for-loop, overwriting any data you had previously written. Move new Array() (or better [], the array literal) outside the loop

Dennis
  • 32,200
  • 11
  • 64
  • 79
1
 var all = [];
    for (i = 0; i <= 4; i++) {
      let element = i;
      all.push(element);
    }
    alert(all[1]);
    alert(all[2]);
    alert(all[3]);
    alert(all[4]);
TechBantu
  • 15
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 12 '22 at 21:27
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Feb 15 '22 at 09:22
0

Try some thing like:

const Coin = [
  "Heads",
  "Tails"];
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

You are recreating the array on each iteration. Try this:

var all = []; // Moved up, and replaced with bracket notation.
var a = 1;
for(i=0;i<4;i++)
{
all[i]=a;
a++;
}

alert(all[1]);
alert(all[2]);
alert(all[3]);
Danny Kirchmeier
  • 1,134
  • 6
  • 14
0

Your issue here is that you're reinstantiating a new Array on each iteration of the loop. So, the first time around, you set a value in that array. The second time around, you redefine the all variable to be a completely new array, which undoes the work you did in the last iteration.

The easiest thing to do is just move var all = new Array() and put it before your loop.

Kevin Ennis
  • 14,226
  • 2
  • 43
  • 44
0

You are redefining your array inside the for loop. You need to define it outside.

var a = 1;
var all = new Array();
for(i=0;i<4;i++)
{
  all[i]=a;
  a++;
}

alert(all[1]);
alert(all[2]);
alert(all[3]);
John W
  • 1,472
  • 1
  • 14
  • 19
0
var a = 1;
var all = new Array();
for(i=0;i<4;i++)
{


all[i]=a;
a++;
}

alert(all[0]);
alert(all[1]);
alert(all[2]
snoofkin
  • 8,725
  • 14
  • 49
  • 86
0

You need to put var all = new Array() outside your loop. You're creating a new all[] four times.

var a = 1;
var all = new Array();
for(i=0;i<4;i++)
{
   all[i]=a;
   a++;
}

alert(all[1]);
alert(all[2]);
alert(all[3]);
Eric Bridger
  • 3,751
  • 1
  • 19
  • 34