三元运算符 使用

 //三元运算符
    //三元表达式判断闰年
    var b = 2012;
    var year = b % 4 === 0 && ! b % 100 === 0 ? "闰年" : "平年";
    console.log(year);

    //判断奇数偶数
    // var a = prompt("输入你要判断的数");
    var a = 3;
    var res = a % 2 === 0 ? "偶数" : "奇数";
    console.log(res);

    //判断是否满18岁
    var age = 17;
    var ret = age>=18 ? "成年" : "未成年";
    console.log(ret);

    //找最大值
    var max1 = 30;
    var max2 = 20;
    var max = max1 > max2 ? "max1比较大" : "max2比较大";
    console.log(max);

三元运算符 使用_第1张图片

你可能感兴趣的:(前端基础)