[JavaScript-03]IF 三元表达式 逻辑运算 == ===

1. 语句

// if 语句
let x = 10;
if (x == 10) 
{
    console.log('x is 10')
};

// if else if  else
x = 20;
if (x === 10) 
{
    console.log('x is 10');
} else if (x > 10) 
{
    console.log('x is greater than 10');
} else
{
    console.log('x is less than 10');
}

// 三元表达式
const xxx = 9;
// ?后面为真 :为else
const color = xxx > 10 ? 'red' : 'blue'
console.log('xxx color:',color);

2. 其他应用

// == 不管类型只管值 === 类型和值必须一致
if (x === '10') {
    console.log('x is 10');
};

// 逻辑运算
// false:undefined,0,"",null,false
// || 或
const xx = 11;
if ( xx <6 || xx >10)
{
    console.log('逻辑(或):成立');
}else
{
    console.log('逻辑(或):不成立');
}

// && 与
const yy = 9;
if (yy >1 && yy < 10)
{
    console.log('逻辑(与)成立');
}else
{
    console.log('逻辑(与)不成立');
}

END

posted @ 2022-11-09 23:19  LeoShi2020  阅读(39)  评论(0)    收藏  举报