0

If you run this code you get 599052. It does this for any number this length until it contains an 8 or 9 generally. Also if the starting number is not a 0 it will work. This really stumped me and I scoured the web to no avail. I'm hoping someone here has some insight into what is going on here.

var addd = function (num) {
  return num;
};

addd(02222014);
user2745054
  • 11
  • 1
  • 3
  • 7
    That's because numbers with a leading zero in JavaScript are considered octal. – j08691 Feb 22 '15 at 00:08
  • add "use strict" at the top to fix it. – dandavis Feb 22 '15 at 00:12
  • @dandavis Then it will throw `SyntaxError: octal literals and octal escape sequences are deprecated`. Integers can't begin with a leading zero, but implementations may (in non-strict mode) parse them as LegacyOctalIntegerLiteral. – Oriol Feb 22 '15 at 00:20
  • Octal numbers are 0-7, which have 3 bits. having an 8 or 9 in there makes the number NOT octal, since 8 and 9 take 4 bits each. That would explain why without the 8 or 9 and with the leading zero you get "weird" numbers, but without the leading zero or with an 8 or 9 you get "normal" numbers. – Reed Feb 22 '15 at 00:28
  • Thanks @j08691 So is there a way to revert base 8 in the function or is this one of those never allow an integer to start with zero moments? I'm working with dates so naturally some months/days need a zero in front and I can't neccissarily do math with strings. – user2745054 Feb 22 '15 at 00:43

0 Answers0