Coerce to number

后端 未结 4 949
半阙折子戏
半阙折子戏 2020-12-15 17:35

I have often seen the trick

after = +after;

to coerce the variable after to a number. Reading through the Node.JS source I fou

4条回答
  •  青春惊慌失措
    2020-12-15 18:24

    Yes, however note that it is only the unary + operator that does this. I.e. 10 + "10" will give you "1010".

    A perhaps less error prone option is to use what asm.js does:

    10 + ("10"|0)
    

    Although on the down-side it does require the brackets. It should be the fastest option in any case (probably equal to unary +).

提交回复
热议问题