javascriptjquerysonarqube

What's the purpose using the assignments symbol in if statement?


Most responses indicate the symbol = is used as an assignment operator.

Indeed, this aligns with what I've learned while studying programming.

I encountered this issue when using SonarQube scanning software: CWE-481: Assigning instead of Comparing

As shown above, it's a problem found in an older version of jQuery, and an excerpt reads as follows:

enter image description here

The assignment in this segment is located elsewhere:

enter image description here

What's the purpose of this approach?

Is there always be TRUE conditions? (if I did not misunderstand).

Is this approach for code organization purposes,

does it aid in future maintenance and code readability, or is it simply an error in usage?


Solution

  • function ifAssignment() {
      let a,b,c,d;
      if((a = true)) {
        console.log("a", a);
      }
      if((b = false)) {
        console.log("b", b);
      }
      if((c = "c")) {
        console.log("c", c);
      }
      if((d = "")) {
        console.log("d", d);
      }
    }
    
    ifAssignment()

    Results show that assignment in if control can be either true or false.