const groceries = ["banana", "apple", "peanuts"];
if (groceries.indexOf("banana")) {
console.log("We have to buy bananas!");
} else {
console.log(`We don't have to buy bananas!`);
}
我们传递了一个状态 groceries.indexOf("banana")
给if条件语句。groceries.indexOf("banana")
返回 0
, 一个 falsy 的值。因为if条件语句的状态为 falsy,else
块区内的代码执行,并且 We don't have to buy bananas!
被输出.