Else statement not executed javascript

后端 未结 5 1664
执念已碎
执念已碎 2021-01-27 10:25

Hey Guys in the below code every time the if statement is executed no matter what the condition is.

function addRow(tableID) {

            var table = document.         


        
5条回答
  •  独厮守ぢ
    2021-01-27 10:45

    This is why I love yoda conditionals. You're assigning a value (=) rather than evaluating it ==.


    EDIT As suggested, I may point out that the order of checks in this particular statement is wrong. The check whether an element #select_degree exists should naturally occur before attempting to modify its properties.

    However, given the element exists, the condition would still always evaluate true since the return value of the assignment (=) is used as an argument. As advocator for yoda conditionals, might I point out their advantage: for the following statement a syntax error would have been raised:

    if ('hidden' = element.style.visibility)
    {
    // ...
    }
    

提交回复
热议问题