i'm new to JavaScript. Im trying to make a loop that prints every month and also numbers them, but i end up getting a concatenation of the numbers. here's my code
var months = ['january', 'february', 'march','april',
'may', 'june', 'july', 'august',
'september', 'october', 'november, 'december'];
for(i in months){
document.write((i+1)+'.- '+months[i]);
document.write('<br >')
}
and my output is like this:
01.- janury
11.- february
21.- march
.... etc
for into iterate over arrays – it’s for iterating over properties of objects.for (var i = 0; i < months.length; i++)to match the existing code, but really, you need to find a more up-to-date resource for learning JavaScript. Anything that teachesdocument.writeis probably outdated.for(i in obj)iterates keys, and they are Strings. When used on Arrays, they are still Strings. That's why they "concatenate"... + the order is not guaranteed. Try (+i+1).for inloop with a cast to number.)