Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading