1

Is there a way to set multiple things in an If statement? For example in the below code, I want to set both status and icon variables if something is true or false. This code is not working and I'm suspecting it's because && doesn't compute, but couldn't find a solution. Any suggestions?

var status;
var icon;
if(x.life > 0) {
    status = "Yes" && icon == "fa-check-circle-o"
} else {
    status = "No" && icon == " fa-times-circle-o"
}
2
  • Do you want to check the values or set the values ? Commented Apr 4, 2017 at 2:50
  • I want to set the values Commented Apr 4, 2017 at 2:51

5 Answers 5

2

Is there a way to set multiple things in an If statement? For example in the below code, I want to set both status and icon variables if something is true or false.

Sure, it is possible, However, it's not working currently because you're not correctly assigning the values. use a single ( = ) for assignment and double ( == ) for comparison (will return boolean).

Try this:

var status;
var icon;
if(x.life > 0) {
    status = "Yes";
    icon = "fa-check-circle-o";
} else {
    status = "No";
    icon = " fa-times-circle-o";
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Dave sure, would really appreciate it if you mark the answer as accepted if it has helped.
haha will do, it won't let me mark correct for another 5 min.
@Dave sure, no problem :).
1

Make two separate statement instead of using && and use = instead of == when assigning value to variable.

var status;
var icon;
if(x.life > 0) {
   status = "Yes"; 
   icon = "fa-check-circle-o"
} else {
   status = "No";
   icon = " fa-times-circle-o";
}

Comments

1

There is no need to use && here which will make your statement a condition. And == will check for equality not compare items

var status;
var icon;
if(x.life > 0) {
    status = "Yes";
    icon = "fa-check-circle-o";
} else {
    status = "No";
    icon = " fa-times-circle-o";
}

Comments

1

You have to separate them with semicolon or enter.

   var status;
   var icon;
   if(x.life > 0) {
       status = "Yes";
       icon == "fa-check-circle-o";
   } else {
       status = "No";  
       icon == " fa-times-circle-o";
   }

Note that && is commonly used for boolean arguments;
example:

   if(x.life > 0 && y.life > 0) {
       status = "Yes";
       icon == "fa-check-circle-o";
   }

1 Comment

"Note that && is only used for boolean arguments" - No it's not. You can use it with any types.
0

If you want to set status based on the value of icon (since you have == in the original code):

var status;
var icon;
if (x.life > 0) {
    if (icon == "fa-check-circle-o") {
        status = "Yes";
    }
} else {
    if (icon == "fa-times-circle-o") {
        status = "No";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.