lundi 18 octobre 2021

JavaScript "if" not working properly when conditioning an object's boolean value [closed]

My code:

document.addEventListener("DOMContentLoaded", function () {
  document.querySelector("#y").onclick = () => {
    y = [
      { name: "A", adult: true },
      { name: "B", adult: false },
    ];
    y.forEach((person) => {
      para = document.createElement("p");
      document.querySelector("#add").append(para);
      para.innerHTML = `${person.adult}`;
      if (`${person.adult}`) {
        para.style.color = "red";
        console.log(`${person.adult}`);
      }
    });
  };
});

Here, when person's adult property is true, then and only then it should colored the text red. But both times, the person's adult property is true or false, it colors the text red.

enter image description here

HOW TO FIX THIS PROBLEM ?

Aucun commentaire:

Enregistrer un commentaire