One sentence lets you understand the ternary operation

The general form of ternary operation is divided into the following two types

// 第一种

// 如果str不等于null或undefined 那就让str等于'有' 否则str等于'无'

str?'有':'无'  即 str!=null||undefined?'有':'无'



// 第二种

// 如果name等于'小明' 那就等于'小明' 否则不管name原来是什么 我不管 现在就让name等于'小红'

name=='小明'?'小明':'小红'


// 如果number大于数字3 那就让number等于 '大' 否则让number等于 '小'

number>3?'大':'小'


// 如果score小于数字60 那就让result等于 '不及格' 否则让result等于 '及格'

score<60?result='不及格':result='及格'

After understanding the above code, the following are all repeated, no need to waste time reading

The first type is generally abbreviated and fully written as follows

// 第一种
str!=null||undefined?'有':'无'

Interpretation: Determine whether the variable str is not null or undefined

Did you feel a little convoluted when you first touched it? (Just go around)

The second is to judge whether the previous part is true

// 第二种


// 如果name等于'小明' 那就等于'小明' 否则不管name原来是什么 我不管 现在就让name等于'小红'

name=='小明'?'小明':'小红'




// 如果number大于数字3 那就让number等于 '大' 否则让number等于 '小'

number>3?'大':'小'




// 如果score小于数字60 那就让result等于 '不及格' 否则让result等于 '及格'

score<60?result='不及格':result='及格'

true → display the former (what follows immediately after the ? )

false → display the latter (followed by :)

おすすめ

転載: blog.csdn.net/ICUiseeyou/article/details/130294036