1

I have a function where I passes the value dynamically

<a href="javascript:void(0);" onclick="searchError(0011)">0011</a>

In javascript am just passing this value and it returns me 9

JS

function searchError(s){
        alert(s);           
}

Need help to understand why ?

I fixed it by quoting the value like

<a href="javascript:void(0);" onclick="searchError('0011')">0011</a>

JS Fiddle

rgettman
  • 176,041
  • 30
  • 275
  • 357
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
  • Possibly interesting: http://stackoverflow.com/questions/2803145/is-there-0b-or-something-similar-to-represent-a-binary-number-in-javascript Top answer has an interesting comment: `Technically, JavaScript doesn't feature octal notation, although most (all?) browsers support it. It's explicitly disallowed in strict mode.` – AntonH Jan 09 '15 at 23:51

1 Answers1

3

0011 is an octal number since it has a leftmost 0 so its equal to 0 x 82 + 1 x 81 + 1 x 80 = 9. Originally the value was interpreted as a numeric. Enclosing it in quotes caused it to be treated as a String literal.

Reimeus
  • 158,255
  • 15
  • 216
  • 276