Home JavaScript Document JavaScript Document Definition
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.
Syntax
JavaScript Syntax Common Document calls beginners use first:
const title = document.title;
const app = document.querySelector("#app");
const btn = document.createElement("button");
btn.textContent = "Save";
app.appendChild(btn); Reference
Core Ideas Ideas every JavaScript Document tutorial builds on:
Idea Example Remember Page root documentEntry to the DOM tree Find one document.querySelector("#app")CSS selector, first match Find id document.getElementById("app")Id only, no # Create document.createElement("p")New element, not yet in page Title document.titleTab / window title text Ready document.bodyNull until body exists
Code
JavaScript Examples Three short programs that show how Document works in a JS tutorial.
1. title 2. query 3. create Example 1 — Read and set the page title document.title is the text in the browser tab.
document.title = "Dashboard";
console.log(document.title); How It Works Assignment updates the tab title.Reading title returns the current string.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.
// HTML: <main id="app"></main>
const app = document.querySelector("#app");
console.log(app && app.id); How It Works #app matches the element with that id.querySelector returns the element node.If nothing matches, the result is null. Example 3 — Create and append a button createElement builds a node. appendChild places it in the tree.
const btn = document.createElement("button");
btn.textContent = "Save";
document.body.appendChild(btn);
console.log(btn.textContent); How It Works createElement("button") makes a button not yet on the page.textContent sets the label.appendChild inserts it under body.Continue
Next: Document Guides After you know what Document is, open the constructor and the method and property pages.
Look up Why Constructor How 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 What is document in JavaScript? 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.
What is the difference between document and window? 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.
When is document.body available? 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.
Should I use querySelector or getElementById? getElementById is fine for a single id. querySelector is more flexible because it accepts any CSS selector. Both return null when nothing matches.
Where should I go next? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion