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

前端 未结 18 1649
感动是毒
感动是毒 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

提交回复
热议问题