kembali ke pelajaran
Materi ini hanya tersedia dalam bahasa berikut: عربي, English, Español, Français, Italiano, 日本語, 한국어, Русский, Українська, 简体中文. Tolong, bantu kami menerjemahkan ke dalam Indonesia.

What's in the nodeType?

pentingnya: 5

What does the script show?

<html>

<body>
  <script>
    alert(document.body.lastChild.nodeType);
  </script>
</body>

</html>

There’s a catch here.

At the time of <script> execution the last DOM node is exactly <script>, because the browser did not process the rest of the page yet.

So the result is 1 (element node).

<html>

<body>
  <script>
    alert(document.body.lastChild.nodeType);
  </script>
</body>

</html>