JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Review For Loops and Loop Exiting

Hui Zheng
PRO
Hui Zheng
Pro Student 1,234 Points

break statement

I recently came across this example online when I was looking for more examples on break statement. After I saw the result it generated, I didn't fully understand the mechanics of this code. I didn't what was the purpose for declaring a variable and assigning an empty space to it ---> var text = " "; and how this got integrated into the code. Also I thought the number 3 was included since i was set to equal to 3 ---> i === 3, but it wasn't. var text = " "; var i; for (i = 0; i < 5; i++) { if (i === 3) { break; } text += "The number is " + i + "<br>"; document.write(text); } The result also had some repeated statements, I didn't understand it---> The number is 0 The number is 0 The number is 1 The number is 0 The number is 1 The number is 2

2 Answers

Hui Zheng
PRO
Hui Zheng
Pro Student 1,234 Points

So declaring a variable with an empty string is to break up a lengthy statement into a few smaller steps? Sometimes the user can concatenate multiple things as the literal value of a variable, so the empty string is to help breaking it down to few lines of code instead of lumping everything together into one single statement? Thanks for the help again professor

Steven Parker
Steven Parker
170,315 Points

Setting an initial value is necessary to be able to add an additional string on by concatenation. It cannot be done with a single statement.

But in this particular code, the concatenation is not needed. So it can be replaced with an ordinary assignment, which would not require an initial value to be set.

If your original question has been answered, you can mark the question solved by choosing a "best answer".
And happy coding!

Steven Parker
Steven Parker
170,315 Points

Text must start with something in it, because it gets concatenated later ("text += ..."). But the space wasn't necessary, an empty string ("") would have been fine.

This is unrelated to the "break" statement. What the "break" does is to stop the loop before it prints the "3" message.

The repetition is caused by that concatenation I mentioned to start with. Instead of creating a new message, it adds on to everything that was output before and prints it all again. Depending on where you saw this, it could have been a mistake by the poster, or a deliberate example to show what can go wrong when the wrong operator is used.

Hui Zheng
Hui Zheng
Pro Student 1,234 Points

What was the purpose for creating an empty string for it to get concatenated later? Couldn't it be done in one single statement? I noticed in an example from one of the videos, the instructor also declared a variable and assigned it an empty string at the beginning. But what was the reason for doing that, I didn't quite understand it.

Steven Parker
Steven Parker
170,315 Points

Concatenation adds on to what is already there, so it's important that you start with a string (even an empty one). But in this case, the concatenation also causes the duplicate about, so it could be replaced with a simple assignment. Then the initial value would not be needed.