1

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?

3
  • 4
    you changed the operator too from += to = Commented Sep 17, 2014 at 22:47
  • You removed the + sign before the = sign. Therefore you're creating a new string everytime instead of appening more content to it. Commented Sep 17, 2014 at 22:47
  • 1
    @RomainBraun—strictly, a new string is created every time regardless of = or +=. ;-) Commented Sep 17, 2014 at 22:57

4 Answers 4

2

There are two things here you must understand:

  • =
    It is an assignment operator. The operation var x = 'something' means that any value inside x will be forgotten, and replaced by the new value 'something'.

  • +=
    It is a binary operator. It increments the value of a variable, so x += 'something' will add the value 'something' to whatever value was already inside the x variable. It's the same as x = x + 'something'

So, don't initializing a value for the variable, would just make it not having anything to add to:

var msg;

msg += 'Round' //This would give you 'undefinedRound'

Now, in your case, you removed the + sign, so you used a simple assign operator =. Every time the for loops, it will reset the variable's value.

Sign up to request clarification or add additional context in comments.

Comments

2

Making the following declaration

var msg;

DOES make msg == undefined initially, but the problem is that second line you changed:

msg = 'Round ' + roundNumber + ': ';

This causes msg to be equal to ONLY THAT value on the right side of the value assignment. By using += rather than just =, you are continuously concatenating onto previous values of msg.

Comments

1
msg += 'Round ' + roundNumber + ': '; //update the msg by adding previus value plus current value
msg = 'Round ' + roundNumber + ': ';  //update the msg by adding only the current value. Just like assigning new value in the msg variable.

Comments

0

The issue is at the following line:

msg = 'Round ' + roundNumber + ': ';

The line should actually read:

msg += 'Round ' + roundNumber + ': ';

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.