DOM Concepts

<!-- index.html -->
<body>
  <div id="container"></div>

  <script>
    // Select an existing element by its ID
    const container = document.querySelector('#container');

    // Create a new <p> element
    const paragraph = document.createElement('p');
    paragraph.textContent = "Hello, DOM!";

    // Append the paragraph to the container
    container.appendChild(paragraph);
  </script>
</body>

createTextNode

In a browser, the document object is a global object that represents the DOM. window and document are global objects and can be accessed directly.

1️⃣ window in the Browser

Example: Accessing window directly in a browser

javascript
CopyEdit
console.log(window); // Logs the global `window` object
console.log(window.innerWidth); // Logs the browser's viewport width
console.log(window.location.href); // Logs the current URL


2️⃣ document in the Browser