search

Home  >  Q&A  >  body text

javascript - Judgment condition question

if(+g == 1){
    //do something
    }

How to explain g == 1 in this code?

PHP中文网PHP中文网2808 days ago431

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-05-19 10:22:52

    Unary operator+表示 正号, g如果是一个非法的数,+g将得到NaN, such as string:

    console.log(+'hello')
    // > NaN
    

    If it is a legal number, then it is 取他的正值:

    console.log(+-3)  // +(-3)
    // > -3
    

    Then determine whether it is equal to 1.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:22:52

    After adding + in front of the variable, the variable will be converted into a number and numerical operations can be performed

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-19 10:22:52

    The meaning of converting variables into numbers.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:22:52

    reply
    0
  • Cancelreply