search

Home  >  Q&A  >  body text

javascript - What is the difference between a line of code placed inside a function and outside a function?

//Inside function

function valueAtBit(num, bit) {
  var s = num.toString(2);
     return s[s.length - bit];
 }
undefined
valueAtBit(128,8)
"1"

//Outside the function

128.toString(2)    //对应函数内第一行
VM2471:1 Uncaught SyntaxError: Invalid or unexpected token

Why?

我想大声告诉你我想大声告诉你2752 days ago783

reply all(1)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-28 09:24:31

    js thinks that the 128. you entered is a decimal, so here comes the problem. Decimal toString must not be the same thing. Then there are two ways, one is to add an extra dot, the other is to complete the decimal, and the third one is to add a bracket.

    reply
    0
  • Cancelreply