我写了这段代码..
let str = "apple";
if (str[0] === "a" && str.lenght > 3) {
console.log("good string");
} else {
console.log("not a good string");
}
两个值都是 true,但它返回 false 值并打印 else 语句。
两个值都是 true,我认为它会打印 if 语句,但在每种情况下都会打印 else 语句。
在您的代码中,条件中有一个拼写错误。您写的是 str.lenght 而不是 str.length。此外,您已正确识别出 str 的第一个字符是“a”,因此逻辑 AND (&&) 运算符也会检查第二个条件。然而,由于拼写错误,str.lenght 未定义,这是假的,因此条件评估为假。这是更正后的代码:
let str = "apple";
if (str[0] === "a" && str.length > 3) {
console.log("good string");
} else {
console.log("not a good string");
}