How do you use the ? : (conditional) operator in JavaScript?

前端 未结 18 1602
感动是毒
感动是毒 2020-11-21 05:48

In simple words, what is the ?: (conditional, "ternary") operator and how can I use it?

相关标签:
18条回答
  • 2020-11-21 06:01

    If you have one condition check instance function in javascript. it's easy to use ternary operator. which will only need one single line to implement. Ex:

        private module : string ='';
        private page:boolean = false;
        async mounted(){
         if(this.module=== 'Main')
        {
        this.page = true;}
        else{
        this.page = false;
        }
    }
    

    a function like this with one condition can be written as follow.

    this.page = this.module=== 'Main' ?true:false;
    

    condition ? if True : if False

    0 讨论(0)
  • 2020-11-21 06:02

    It is called the ternary operator

    tmp = (foo==1 ? true : false);
    
    0 讨论(0)
  • 2020-11-21 06:03

    Most of the answers are correct but I want to add little more. The ternary operator is right-associative, which means it can be chained in the following way if … else-if … else-if … else :

    function example() {
        return condition1 ? value1
             : condition2 ? value2
             : condition3 ? value3
             : value4;
    }
    

    Equivalent to:

    function example() {
        if (condition1) { return value1; }
        else if (condition2) { return value2; }
        else if (condition3) { return value3; }
        else { return value4; }
    }
    

    More details is here

    0 讨论(0)
  • 2020-11-21 06:07

    We can use with Jquery as well as length as below example :

    Suppose we have GuarantorName textbox which has value and want to get firstname and lastname- it may be null. So rathar than

            var gnamesplit = $("#txtGuarantorName").val().split(" ");
            var gLastName = "";
            var gFirstName = "";
            if(gnamesplit.length > 0 ){
               gLastName  = gnamesplit[0];        
            }
            if(gnamesplit.length > 1 ){
               gFirstName = gnamesplit[1];        
            }
    

    We can use below code with Jquery with minimum code

        
    
        var gnamesplit = $("#txtGuarantorName").val().split(" ");
        var gLastName = gnamesplit.length > 0  ? gnamesplit[0] : "";
        var gFirstName =  gnamesplit.length > 1  ? gnamesplit[1] : "";
        $("#txtLastName").val(gLastName);
        $("#txtFirstName").val(gFirstName);
        
        
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div >
      Guarantor Name: <input type="text" id="txtGuarantorName" value="ASP.NET Core"  /><br/>
      <br/>
      <br/>
      
      First Name: <input type="text" id="txtLastName" value="ASP.NET Core"  />
      Last Name: <input type="text" id="txtFirstName" value="ASP.NET Core"  />
    </div>

    0 讨论(0)
  • 2020-11-21 06:08
    z = (x == y ? 1 : 2);
    

    is equivalent to

    if (x == y)
        z = 1;
    else
        z = 2;
    

    except, of course, it's shorter.

    0 讨论(0)
  • 2020-11-21 06:08

    The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

    condition ? expr1 : expr2 
    

    If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

    function fact(n) {
      if (n > 1) {
        return n * fact(n-1);
      } else {
        return 1;
      }
      // we can replace the above code in a single line of code as below
      //return (n != 1) ? n * fact(n - 1) : 1;
    }
    console.log(fact(5));
    

    For more clarification please read MDN document link

    0 讨论(0)
提交回复
热议问题