Check if object exists in JavaScript

前端 未结 18 2662
抹茶落季
抹茶落季 2020-12-04 05:26

How do I verify the existence of an object in JavaScript?

The following works:

if (!null)
   alert(\"GOT HERE\");

But this throws a

18条回答
  •  粉色の甜心
    2020-12-04 05:52

    I've just tested the typeOf examples from above and none worked for me, so instead I've used this:

        btnAdd = document.getElementById("elementNotLoadedYet");
        if (btnAdd) {
           btnAdd.textContent = "Some text here";
        } else {
          alert("not detected!");
        }

提交回复
热议问题