Skip to content

妙用Javascript运算符“||”和“&&” #3

Closed
@Wscats

Description

@Wscats
Owner
var a = 1;
        var b = 2;
        if (a < b && a == 1) {
            //console.log("&&");
        }
        //上面改写
        a < b && a == 1 && console.log("&&");
        if (a > b || a == 1) {
            //console.log("||");
        }
        //
        a > b || a == 1 && console.log("||");
        //&&前面为真运行后面 否则不运行
        var c = (a == 1) && 3
        var d = (a == 2) && 3
        console.log(c); //3     取了后面的3
        console.log(d); //false 取了前面的false
        //||前面为假运行后面 否则不运行
        var c = (a == 1) || 3
        var d = (a == 2) || 3
        console.log(c); //true
        console.log(d); //3
        //巧用
        var e = 0;
        var f;
        switch (f) {
            case 5:
                e = 1;
                break;
            case 10:
                e = 2;
                break;
            case 12:
                e = 3;
                break;
            case 15:
                e = 4;
                break;
            default:
                e = 0;
                break;
        }
        var e = (f == 5 && 1) || (f == 10 && 2) || (f == 12 && 3) || (f == 15 && 4) || 0;
        console.log(e); //0

总结理解为
&&遇上假就停止,并返回这个假,相反遇到真就继续执行直到拿最后一个
||遇上真就停止,并返回这个真,相反遇到假就继续执行直到拿最后一个
这样会不会好理解多
注意:非0的整数都为true,undefined、null和空字符串”" 为false。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Wscats

        Issue actions

          妙用Javascript运算符“||”和“&&” · Issue #3 · Wscats/articles