gpt4 book ai didi

javascript - 一个关于条件语句和定义变量的简单问题

转载 作者:行者123 更新时间:2023-11-28 17:02:01 25 4
gpt4 key购买 nike

我想知道1. 为什么控制台抛出特定结果2.如何让控制台根据代码抛出我想要的结果

我尝试过删除 else 条件并添加它,但我陷入困境,因为我不知道代码在想什么。

isMarried = false; 
if (isMarried = false) {
isMarried = 'Nope, not at all! ';
}

console.log(firstName + ' is a ' + age + ' year old ' + job + '. Is he married? ' +isMarried );


//This outputs false for isMarried instead of "Nope, not at all!"

If I add an else like so: 
if (isMarried = false) {
isMarried = 'Nope, not at all! ';
} else {
isMarried = 'Oh yeah';
}

//The same code outputs "Oh yeah." I'm a bit confused why it's happening like this. Any thoughts? 

基本上,我希望计算机将 isMarried 视为错误的 bool 值,如果是这种情况,我想将变量设置为上面看到的字符串。否则,例如,如果我将其更改为 true,它将是一个不同的字符串。

最佳答案

不要在条件语句中使用赋值运算符 (=)。在条件语句中,您需要使用比较运算符(==、!=、>=、< 等)

关于javascript - 一个关于条件语句和定义变量的简单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57135543/

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