JS Dasar: 33. Operator logika di non boolean

by istiq20

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Operator logika di non boolean</title>
  </head>
  <body>
    <script>
      // Operator OR (||)
      console.info("Hello" || ""); // Hello
      console.info("" || []); // []
      console.info("0" || "NOL"); // 0
      console.info(0 || "NOL"); // NOL
      console.info(null || "NULL"); // NULL
      console.info(undefined || "UNDEFINED"); // UNDEFINED
      console.info(0 || false); // false

      const person = {
        firstName: "",
        lastName: "Khannedy",
      };

      const name = person.firstName || person.lastName;
      console.info(name); // Khannedy

      // Operator AND (&&)
      console.info("HELLO" && ""); // ""
      console.info("" && []); // ""
      console.info("0" && "NOL"); // NOL
      console.info(0 && "NOL"); // 0
      console.info(null && "NULL"); // null
      console.info(undefined && "UNDEFINED"); // undefinded
      console.info(0 && false); // 0
    </script>
  </body>
</html>