What does = +_ mean in JavaScript

前端 未结 12 1862
南笙
南笙 2020-11-22 11:37

I was wondering what the = +_ operator means in JavaScript. It looks like it does assignments.

Example:

hexbin.radius = function(_)         


        
12条回答
  •  轮回少年
    2020-11-22 12:34

    +_ is almost equivalent of parseFloat(_) . Observe that parseInt will stop at non numeric character such as dot, whereas parshFloat will not.

    EXP:

        parseFloat(2.4) = 2.4 
    vs 
        parseInt(2.4) = 2 
    vs 
        +"2.4" = 2.4
    

    Exp:

    var _ = "3";
        _ = +_;
    
    console.log(_); // will show an integer 3
    

    Very few differences:

    • Empty string "" evaluates to a 0, while parseInt() evaluates to NaN
    • For more info look here: parseInt vs unary plus - when to use which

提交回复
热议问题