Subscribe via RSS
28Mar/170

Javascript numbers with leading zeroes

Here is yet another public service announcement. I've recently been coding times for this post to track train timetables. Whilst doing so, I had the times in my array fully padded out to make everything easier to read.

i.e. for the southbound passenger timetable, I had the following:

var southbound_pax = [
    0530,0611,0633,0703,0714,0725,0733,0743,0752,0808,0823,
    0830,0841,0859,0917,0928,0944,1006,1029,1053,1109,1128,
    1148,1209,1230,1251,1309,1327,1348,1409,1427,1448,1512,
    1532,1550,1607,1618,1631,1651,1709,1726,1737,1754,1808,
    1818,1831,1844,1902,1915,1925,1937,1955,2014,2029,2050,
    2102,2121,2143,2208,2227,2255,2349
];

All of my testing had been done after 10am, so everything worked fine. This morning I get in and check the timings and it's all really wrong. I whipped up a quick test as I'd seen strange values in the arrays.

> var number_array = [0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0008, 0009, 0010, 0011, 0012];

> number_array
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 10]

Wait, what?... we're good until 0009. Then it goes nuts? A heavy bit of googling produced this warning on w3schools:

Never write a number with a leading zero (like 07).
Some JavaScript versions interpret numbers as octal if they are written with a leading zero.

Ohh.... riiiight... some... Did anyone else know of this feature?
Effectively, if it's octal, then 0009 should throw an error, right? Not just silently work and then flip back to '8' at 0010. Grrr...