gpt4 book ai didi

javascript - If 语句 js 中有多个内容

转载 作者:行者123 更新时间:2023-11-28 14:55:08 25 4
gpt4 key购买 nike

有没有办法在 If 语句中设置多个内容?例如,在下面的代码中,如果某件事是真或假,我想设置状态和图标变量。这段代码不起作用,我怀疑这是因为 && 无法计算,但找不到解决方案。有什么建议吗?

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

最佳答案

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.

当然,这是可能的,但是,它当前不起作用,因为您没有正确分配值。使用单值 ( = ) 进行赋值,使用双值 ( == ) 进行比较(将返回 boolean)。

试试这个:

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

关于javascript - If 语句 js 中有多个内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com