GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 妙用Javascript运算符“||”和“&&” · Issue #3 · Wscats/articles

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

New issue
Copy link
New issue
Copy link
Closed
Closed
妙用Javascript运算符“||”和“&&”#3
Copy link
Labels
notes

Description

@Wscats
Wscats
opened on Apr 15, 2016
Issue body actions
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

  1. Wscats
    added
    notes
    on Apr 15, 2016
  2. Wscats
    closed this as completedon Aug 22, 2019
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

    notes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.