On this site about javascript shorthands, by Sam Deering, a long-hand example is as follows (the site uses a before-and-after format):
var i=0;
while (i<9)
{
//do stuff
i++; //say
}
While I don't understand this, I do understand the short-hand version, which is:
var i=9;
while(i--)
{
//goes until i=0
}
I don't know about if i
is the same in both loops, but assume that they both loop 9 times.
Because I would like to expand my limits and improve my programming skills, what does the while (i<9)
mean, and how can I use it (with other numbers)?
On this site about javascript shorthands, by Sam Deering, a long-hand example is as follows (the site uses a before-and-after format):
var i=0;
while (i<9)
{
//do stuff
i++; //say
}
While I don't understand this, I do understand the short-hand version, which is:
var i=9;
while(i--)
{
//goes until i=0
}
I don't know about if i
is the same in both loops, but assume that they both loop 9 times.
Because I would like to expand my limits and improve my programming skills, what does the while (i<9)
mean, and how can I use it (with other numbers)?
-
5
That's most likely an encoding issue.
<
is the HTML entity for the<
character. The author even mentionswhile (i++<10)
as an alternative, so that's clearly what was intended. – p.s.w.g Commented Jun 27, 2016 at 23:41 - Write your code without shorthands like this. It's work for minifiers. – vp_arth Commented Jun 27, 2016 at 23:45
-
1
You are receiving downvotes because most people think you are doing it very wrong. Unless you use HTML you can ignore them,
<
is a proper way to escape<
in XHTML. Using<
might throw a draconian parse error. – Oriol Commented Jun 28, 2016 at 0:01 - I did not know I was reading a poorly written example. That was why I was asking, I am not asking about my own style, rather about a website's style. – user31415 Commented Jun 28, 2016 at 16:57
2 Answers
Reset to default 3In XML, e.g. XHTML, you must escape <
properly as <
.
In HTML it's not necessary to do so inside script
elements because their contents are parsed in a special way, but XML does not do these nasty things.
For example, you can write index.xhtml
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head><title>Some document</title></head>
<body>
<script type="text/javascript">
var i=0;
while (i < 9) i++;
console.log(i); // 9
</script>
</body>
</html>
Most people don't want to XML-escape JavaScript operators in inline scripts, so they use CDATA sections:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head><title>Some document</title></head>
<body>
<script type="text/javascript">
<![CDATA[
var i=0;
while (i < 9) i++;
console.log(i); // 9
]]>
</script>
</body>
</html>
Your HTML file most probably has some encoding quirks. <
corresponds to the less than symbol <
in HTML entities. So really you can read that as while (i<9)
.