javascript returns 9 for 0011 [duplicate]

北战南征 提交于 2019-12-11 11:36:16

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/27871143/javascript-returns-9-for-0011

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!