当前位置:网站首页>||Interview questions you will encounter

||Interview questions you will encounter

2022-07-05 00:35:00 Front end paper plane

Contented and determined , Gentle and progressive .

The first thing to know is || It's the operator .

We usually use it to make some judgments , For example, when one condition is true , It will return true, When all conditions are false , It will return false, However, is the fact really like this ?

Not always . Look below .

const res = ('' || 0 || 1 || 2)
//1
const res = ('' || 0 || null)
//null
const res = ('' || null || 0)
//0

The above three questions , Output res Result .

The first question is and The difference between the last two questions is The next two questions || The left and right Boolean values are false

Let's do the first one :
Output 1, so , When the conditional Boolean value is true when , The first one will be returned as true Conditions
Look at the second and third questions :
The conditions are false, The last one is returned

summary ,|| Will return to the first Boolean Converted to true Conditions , If not, the last condition is returned .

Here are some data Bollean The result is false, We should see them and regard them as false.

undefined
null
false
0
NaN
"" or ’’( An empty string )

原网站

版权声明
本文为[Front end paper plane]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141118467683.html