Alexandar Targov
Alexandar Targov

Reputation: 119

JavaScript operator ++

i'm having this js code and i can't understand why does the last number is 12 and why it starts counting from 8. Why doen't it strat from 10 and end at 14?

<html>
<body>
<script type="text/javascript">
var k;
for (k=010; k<015; k++)
{
document.write(" "+k);
document.write("<br>");
}
</script>
</body>
</html>

Upvotes: 1

Views: 44

Answers (1)

Pointy
Pointy

Reputation: 413712

The 0 at the start of your numbers causes the language to interpret them as base 8 ("octal") values. 010 is 8, and 015 is 13.

If you remove the leading 0 characters from your numbers, it will work.

The use of a leading 0 to indicate octal dates back to (at least) C.

Upvotes: 7

Related Questions