Alexandar Targov - 27 days ago 5x

Javascript Question

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>

Answer

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.

Source (Stackoverflow)

Comments