The document object
The document object in JavaScript is the main entry point into the DOM (Document Object Model): it represents the whole HTML page loaded in the browser and lets you read, change and control its content from code. The browser creates this object automatically, and it corresponds to the root node of the DOM tree.
Theory
TL;DR
documentis a global object the browser creates automatically when a page loads.- It represents the document as a whole and is the root of the DOM tree.
- Through it you find, create, change and remove nodes, and listen for events.
- Key properties:
documentElement,head,body,title,URL,cookie,forms. - Key methods:
querySelector,querySelectorAll,getElementById,createElement,appendChild,addEventListener. documentlives insidewindow:window.document === document.
Quick example
If the HTML looks like this:
<!DOCTYPE html>
<html>
<body>
<h1>Hello, world!</h1>
</body>
</html>then a DOM structure is created in the browser's memory, and document is the object through which you reach it:
console.log(document.title); // prints the page title
console.log(document.body); // <body>...</body>
console.log(document.URL); // the page address
console.log(document.getElementById); // the function that looks elements upWhat document gives you
document is the main interface for working with the whole page. Through it you can:
- find elements (
getElementById,querySelector,getElementsByClassName); - change their content (
textContent,innerHTML); - create new nodes (
createElement); - remove or move elements (
remove,appendChild); - react to events (
addEventListener); - work with attributes, styles and classes.
Usage examples
Get an element:
const heading = document.querySelector('h1');
console.log(heading.textContent); // "Hello, world!"Change the text:
heading.textContent = 'Good day!';Create and append an element:
const p = document.createElement('p');
p.textContent = 'This is a new paragraph';
document.body.appendChild(p);Change a style:
heading.style.color = 'red';Attach an event handler:
document.querySelector('button').addEventListener('click', () => {
alert('Button clicked!');
});Main properties of document
| Property | What it does |
|---|---|
document.documentElement | <html>, the root element |
document.head | <head> |
document.body | <body> |
document.title | The page title |
document.URL | The current URL |
document.cookie | Access to cookies |
document.forms | Every form on the page |
document.images | Every image |
document.links | Every link (<a>) |
Main methods of document
| Method | Description |
|---|---|
getElementById(id) | Find an element by ID |
getElementsByClassName(name) | Every element with the class |
getElementsByTagName(name) | Every element with the tag |
querySelector(selector) | The first element matching a CSS selector |
querySelectorAll(selector) | Every element matching a CSS selector |
createElement(tag) | Create an element |
createTextNode(text) | Create a text node |
append() / appendChild() | Append an element |
remove() | Remove an element |
addEventListener(event, fn) | Listen for events |
Relation to window
The document object sits inside the global window object:
console.log(window.document === document); // truewindowis the whole browser window: the tab,localStorage, history, timers and so on.documentis only the content of the current page.
In short:
| Question | Answer |
|---|---|
| What it is | The object representing the HTML document |
| Where it comes from | Created automatically when the page loads |
| What it does | Gives control over the DOM |
| Who owns it | window.document |
| Main methods | querySelector, createElement, appendChild, addEventListener |
Common mistakes
- Confusing
documentandwindow.windowis the whole window and the global scope;documentis only the document inside it. - Reaching for
document.bodyfrom a script in<head>withoutdefer. At that momentbodyhas not been parsed yet, and you getnull. - Assuming
document.getElementsByClassNamereturns an array. It returns a live HTMLCollection: it has no array methods and its contents change with the DOM.querySelectorAllreturns a static NodeList. - Using
document.writeafter the page has loaded. The call wipes the whole document; it is a legacy API. - Thinking
documentis available everywhere. It does not exist in Node.js or inside a Web Worker or Service Worker, so an environment check is needed there. - Inserting user text through
innerHTML. That opens the door to XSS; usetextContentfor text.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.