This question already has answers here:
Which Logic Operator Takes Precedence
                            
                                (7个答案)
                            
                    
                3年前关闭。
        

    

我正在尝试检查firstday是否等于5或6,在两种情况下,如果totalDays等于31,则应执行某些操作,为此,哪种说法正确?

代码1:

if (firstday > 5 || firstday > 6 && totalDays == 31){}


代码2:

if (firstday > 5 && totalDays == 31 || firstday > 6 && totalDays == 31) { }

最佳答案

这是正确的答案:

if ((firstday == 5 || firstday == 6) && totalDays == 31){
    //Do something
}

关于javascript - 如何使用||和&&在一个if语句中一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40670778/

10-10 11:54