Getting an "undefined" in the output


#1

//learning stuff
//arrays are not the same length (I know!!)
var learning = ["this", "how", "learn", "program", "javascript"];
var stuff = ["is","I", "how to", "in"];

for(i=0; i <= 4; i++)
{
console.log(learning[i]);
console.log(stuff[i]);
}


#2

so what's the problem if you know that your array 'stuff' is not defined for index 4 ?


#3

i wanted to know if there was a way to remove the "undefined" output for "stuff" array..it spits out "undefined" when i = 4


#4

Yes that's because stuff has no index 4. stuff has 4 elements with the indices 0,1,2,3. As 4 does not exist it is literally undefined.


#5

you can do this

for(i=0; i <= 4; i++)
{
console.log(learning[i]);
if (i < 4)
    console.log(stuff[i]);
}

#6

By doing that will it still print out the "javascript" in the learning array?


#7

What make you think it wouldn't ?