if(+g == 1){ //do something }
How to explain g == 1 in this code?
认证0级讲师
Unary operator+表示 正号, g如果是一个非法的数,+g将得到NaN, such as string:
+
正号
g
+g
NaN
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.
After adding + in front of the variable, the variable will be converted into a number and numerical operations can be performed
The meaning of converting variables into numbers.
Unary operator
+
表示正号
,g
如果是一个非法的数,+g
将得到NaN
, such as string:If it is a legal number, then it is
取他的正值
:Then determine whether it is equal to 1.
After adding + in front of the variable, the variable will be converted into a number and numerical operations can be performed
The meaning of converting variables into numbers.