1

console.log(01) results in 1

But

console.log(011) results in 9

Can someone explain how console.log works with such numbers?

Pranav Kale
  • 629
  • 1
  • 5
  • 13
  • @AvinashMahlawat this isn't C/C++, though. Sure, it's the same concept in this case but it's not a clear dupe. – VLAZ May 17 '19 at 12:48
  • @VLAZ this is related to octal notation, not language specific.So, i am sure it is an answer of your question. – Engineer May 17 '19 at 12:51
  • @AvinashMahlawat yet this notation is deprecated in JavaScript and shouldn't be relied on. It's not exactly language agnostic. – VLAZ May 17 '19 at 12:53

2 Answers2

2

It's not about console.log, a number that starts with 0 is octal notation

farvilain
  • 2,552
  • 2
  • 13
  • 24
  • 2
    Duplicate of https://stackoverflow.com/questions/42571758/console-log-output-in-javascript – LuVu May 17 '19 at 12:47
0

console.log(+"011") // if you use like this it will work

011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.

Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59