JavaScript Document

Beginner
8 min read
Updated: Nov 2026
3 examples
DOM root

Definition and Usage

In the browser, document is the object that represents the loaded web page. It is the entry point to the DOM: elements, text, and attributes your script can read and change.

You find nodes with document.querySelector() and getElementById(). You build new nodes with createElement() and place them with methods on Node and Element.

This page teaches what Document is. Constructor, methods, properties, and events live in the Document guides linked below.

document is already there in the browser.

You usually use the global document. Creating another Document is an advanced case covered on the constructor page.

JavaScript Syntax

Common Document calls beginners use first:

JavaScript
const title = document.title;
const app = document.querySelector("#app");
const btn = document.createElement("button");
btn.textContent = "Save";
app.appendChild(btn);

Core Ideas

Ideas every JavaScript Document tutorial builds on:

IdeaExampleRemember
Page rootdocumentEntry to the DOM tree
Find onedocument.querySelector("#app")CSS selector, first match
Find iddocument.getElementById("app")Id only, no #
Createdocument.createElement("p")New element, not yet in page
Titledocument.titleTab / window title text
Readydocument.bodyNull until body exists

JavaScript Examples

Three short programs that show how Document works in a JS tutorial.

Example 1 — Read and set the page title

document.title is the text in the browser tab.

JavaScript
document.title = "Dashboard";
console.log(document.title);

How It Works

  1. Assignment updates the tab title.
  2. Reading title returns the current string.
  3. This works on the live document in the browser.

Example 2 — Find an element with querySelector

Pass a CSS selector. The first match is returned, or null.

JavaScript
// HTML: <main id="app"></main>
const app = document.querySelector("#app");
console.log(app && app.id);

How It Works

  1. #app matches the element with that id.
  2. querySelector returns the element node.
  3. If nothing matches, the result is null.

Example 3 — Create and append a button

createElement builds a node. appendChild places it in the tree.

JavaScript
const btn = document.createElement("button");
btn.textContent = "Save";
document.body.appendChild(btn);
console.log(btn.textContent);

How It Works

  1. createElement("button") makes a button not yet on the page.
  2. textContent sets the label.
  3. appendChild inserts it under body.

Next: Document Guides

After you know what Document is, open the constructor and the method and property pages.

Look upWhy
ConstructorHow Document instances are created
querySelector()Find the first matching element
createElement()Build a new element node
getElementById()Find by id attribute

Full lists: Document constructor.

Frequently Asked Questions

In a browser page, document is the object that represents the HTML document. Scripts use it to find, create, and update elements in the DOM.
window is the browser window (and the global object). document is the page loaded inside that window. window.document is the same object as document.
After the body element exists. Scripts in the head may run too early unless you wait for DOMContentLoaded or place the script at the end of body.
getElementById is fine for a single id. querySelector is more flexible because it accepts any CSS selector. Both return null when nothing matches.
Open the Document constructor guide, then Element and Node for working with the nodes you find.

Open the Document constructor

Start with the constructor page, then browse methods and properties.

Document constructor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful