Q:

Javascript - Display array elements

I am trying to run a simple Javascript to display the elements of an array that I create by splitting a string.  The code is as follows:
<! DOCTYPE html>
<html>
<body>

<h1>JavaScript Loops</h1>

<p id="demo"></p>

<script>
  var text = "01/01/2016";
  var parts = text.split("/");
  var i;
  for (i = 0; i < parts.length; i++) {
    var x += parts[i] + "<br>";
  }
  document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

The above code does not display anything on the browser and I can't for the life of me figure out where the error is.  Can someone advise? 

A:

This line is tricky, so any JavaScript compiler would complaint:
var x += parts[i] + "<br>";

Uncaught SyntaxError: Unexpected token +=

You can't declare a variable with the "+=" assignment operator. 
Let's rewrite this assignment:
var x = x + parts[i] + "<br>";

When it works, the result is the same.  Only that the first time this runs, x is undefined, not initialized yet.  Concatenating undefined to any a string will contain the string undefined in the result. 
But does it re-declare the variable?  No, because a concept called hoisting, any variable declaration, even inside a loop, is pushed to the top of the scope, in this case the global scope. ( We're not talking about let and const)
To solve that, before you read from this variable, you need to definitely assign to it, i.e. initialize. 
var x = '';

Later you'll be able to use the operator +=, and then you don't need to use the var keyword.