'In for loop of JavaScript, when we iterate, how come it gives result from 0 to 9 in given code when x is a string. It should concat rather [closed]
let x = ""; //Here we have defined x as a string*
for (let i = 0; i < 10; i++) {
x += i + "<br>"; // and here we are adding string to a number so, the result should be concatenation.
}
document.getElementById('demo').innerHTML = x; //
But when we get result, we get numbers from 0 - 9. How Come?
Solution 1:[1]
Because you started with i being 0, and you iterate so long as i is less than 10. So it starts at 0, and once i reaches 10 it exits the loop without executing the code in your loop for the value 10.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CambridgeCro |
