Skip to content

输出什么?

Posted on:2021年7月3日 at 22:30
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! 被输出.

原文转自:https://fe.ecool.fun/topic/22d88306-35f1-4538-898c-2ce3e5303fea