I'm currently studying javascript on my own and would like to see if anyone could help me clarify this questions I have. I'm currently learning loops and came across this code in the book:
var scores = [34, 45, 66, 1023, 1030, 'Done!'];
var arrayLength = scores.length;
var roundNumber = 0;
var msg = '';
var i;
for (i = 0; i < arrayLength; i++) {
roundNumber = (i + 1);
msg += 'Round ' + roundNumber + ': ';
msg += scores[i] + '<br />';
}
document.getElementById('answer').innerHTML = msg;
<div id="answer"></div>
Now that loops through the array and returns all numbers in the array. But If i were to change:
var msg = ''; to var msg;
msg = 'Round ' + roundNumber + ': ';
it only returns the last item in the array. Why does that affect it? How does making the msg variable as null change everything?
+=to=+sign before the=sign. Therefore you're creating a new string everytime instead of appening more content to it.=or+=. ;-)