if(+g == 1){
//do something
}
How to explain g == 1 in this code?
巴扎黑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.
伊谢尔伦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