Reputation: 1277
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"
}
Upvotes: 1
Views: 2974
Reputation: 144
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";
}
Upvotes: 1
Reputation:
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";
}
}
Upvotes: 0
Reputation: 56489
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";
}
Upvotes: 2
Reputation: 16364
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";
}
Upvotes: 1
Reputation: 68933
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";
}
Upvotes: 1