What does a + prefix do in this context?

后端 未结 5 577
栀梦
栀梦 2021-01-27 04:23

Here is a line of code from underscore. What is that plus prefix for in this line?

if (obj.length === +obj.length) { // plus prefix?
5条回答
  •  北海茫月
    2021-01-27 04:39

    The plus prefix converts the variable into a number. Basically, the obj.length === +obj.length is a sanity check that obj.length really is a number. If the obj.length was not a number, and for example a string "foo", then "foo" === +"foo" would equate to false since +"foo" comes out as NaN.

提交回复
热议问题