Im reading the tutorials here .
I am getting confused trying to understand some of this example, why is the variable declared as nothing and what does the ,i indicate
var x="",i;
and also why do you use
x=x
at the beginning of the line?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop from 1 to 6, to make HTML headings.</p>
<button onclick="myFunction()">Try it</button>
<div id="demo"></div>
<script>
function myFunction()
{
var x="",i;
for (i=1; i<=6; i++)
{
x=x + "<h" + i + ">Heading " + i + "</h" + i + ">";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
var x="",i;
This translates as
var x = "";
var i;
which simply declares those variables within the current scope.
x=x + ...
This means replace the value of x
with the value of the expression to the right of the =
sign. In this case, you are concatenating a string to the end of the current value of x
.
var x="",i;
is the same as
var x = "";
var i;
Declare variables in the form of
var a=1,
b=2,
c=3;
is common and make the code style looking clear and easy to read.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.