Example 1 — Read document.title
Log the current page title from JavaScript.
console.log("Page title:", document.title); How It Works
The getter returns whatever text is in the document’s <title> element.

Document.title is a get/set instance property that holds the page title shown in the browser tab, bookmarks, and history. Learn how it syncs with the <title> element, how to update titles dynamically, SPA patterns, notification badges, and five examples with try-it labs.
Read / write
String
Browser UI
<title> element
SPAs / alerts
Baseline widely
Every HTML page has a title—the short label users see in the browser tab, bookmark list, and search results. You usually declare it with a <title> tag inside <head>, but JavaScript can read and change it at any time through document.title.
MDN: the title property gets or sets the current title of the document. When you assign a new string, the browser updates the first <title> element (or creates one in <head> if missing). That change is reflected immediately in the tab bar.
The same title string powers the browser tab, default bookmark name, history entry label, and (for crawlers) the primary page heading signal. Keep titles concise, unique, and descriptive.
Related Document tutorials: head, documentElement, readyState, Document constructor.
Document.titleA get/set string instance property on every Document object.
<title> element, or inserts one in <head> (MDN).<title> in the page source.// Read the current title
document.title
// Set a new title
document.title = "My Page — CodeToFun"; A string representing the document title (MDN).
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page — CodeToFun</title>
</head>
<body>...</body>
</html> | Goal | Code / note |
|---|---|
| Read title | document.title |
| Set title | document.title = "New title" |
| Get title element | document.querySelector("title") |
| Append badge | document.title = "(3) " + baseTitle |
| SPA route change | Set title when view changes |
| MDN status | Baseline Widely available |
Four facts about document.title.
stringGet / set
tab barBookmarks
<title>MDN
BaselineStandard
document.title vs querySelector("title")document.title | querySelector("title") | |
|---|---|---|
| Returns | String (title text) | HTMLTitleElement or null |
| Update title | document.title = "..." | el.textContent = "..." |
| Creates missing title | Yes (MDN) | No — you must create the element |
| Best for | Simple read/write | DOM inspection, attributes, events |
Examples follow MDN Document: title. Each includes a try-it lab you can run in the browser.
Read and write the document title.
document.titleLog the current page title from JavaScript.
console.log("Page title:", document.title); The getter returns whatever text is in the document’s <title> element.
document.titleChange the tab title with a single assignment (MDN).
document.title = "Hello from JavaScript!";
console.log("New title:", document.title); Assigning updates the <title> element and the browser tab label instantly.
Practical patterns for real pages and apps.
<title> ElementConfirm document.title stays in sync with the DOM.
const titleEl = document.querySelector("title");
console.log({
fromProperty: document.title,
fromElement: titleEl ? titleEl.textContent : null,
inSync: document.title === titleEl?.textContent
}); Both accessors read the same underlying title text when the page has a single <title> element.
Prefix the title with an unread count—common in chat and inbox apps.
const baseTitle = document.title.replace(/^\(\d+\)\s*/, "");
const unread = 3;
document.title = unread > 0
? `(${unread}) ${baseTitle}`
: baseTitle;
console.log(document.title); Store the base title once, strip any existing badge, then prepend (count) when there are unread items.
Temporarily change the title (e.g. “Saved!”) and revert after a delay.
const original = document.title;
document.title = "Saved!";
console.log("Flashed:", document.title);
setTimeout(() => {
document.title = original;
console.log("Restored:", document.title);
}, 2000); Save the current title before overwriting it so you can restore the tab label after a short UX message.
(3) in the tab for new messages.document.title to verify page identity.document.title WorksThe parser reads <title> in <head> when the page loads.
document.title getter returns that string (MDN).
Setter changes the <title> element and the browser tab (MDN).
Tab, bookmarks, history, and crawlers all use the same title string.
document.title updates or creates the first <title> element (MDN).og:title and other meta tags are separate—update them too if you care about social previews.Document.title is marked Baseline Widely available on MDN. Logos use the shared browser-image-sprite.png sprite from this project.
Get/set string — page title for browser tabs, bookmarks, and history.
Bottom line: Use document.title to read or update the page title. It syncs with the title element and works in every modern browser.
Document.title is the simplest way to read and change the page title from JavaScript. It returns a string, updates the <title> element when you assign a new value, and immediately reflects in the browser tab. Use it for SPAs, notification badges, and short UX messages—always keep a copy of the original title when you plan to restore it.
Continue with URL, ownerDocument, head, timeline, or the JavaScript hub.
<title>document.title when SPA routes changeog:title updates automaticallydocument.titleThe page title string for tabs, bookmarks, and history.
String
Get/setBrowser UI
Live<title>
Sync(3) prefix
UXBaseline
StandardIf a page has no <title> element, assigning document.title creates one inside <head> automatically (MDN). That means you can set a title entirely from JavaScript even when the HTML forgot to include it.
Learn how to read the document location string with document.URL.
6 people found this page helpful