JavaScript Document dir Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Document.dir is a string for the document’s text directionality: "ltr" (left to right) or "rtl" (right to left) — MDN.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.dir. Start with this line.

JavaScript
document.dir
CallWhat it does
document.dirPrimary signature.

Built into the browser. Read or assign document.dir — no import required.

Library declarations
JavaScript
document.dir

Parameters

ParameterDescription
"ltr" / "rtl"Required. Allowed API values (MDN)

Return Value

Return ValueDescription
*The current value of document.dir.

JavaScript Examples

Example 1 — Read document.dir

Log the current document direction string.

JavaScript
console.log(document.dir);
// typically "ltr" or "rtl"
Try It Yourself

How It Works

  1. Access the property. document.dir is a string on Document.
  2. See the current default. Many English pages report "ltr".
  3. RTL pages differ. Arabic/Hebrew UIs often start as "rtl".
  4. No side effects. Reading alone does not change layout.

Example 2 — Set "rtl" Then "ltr"

Assign MDN’s documented values to switch direction.

JavaScript
document.dir = "rtl";
console.log(document.dir);  // "rtl"

document.dir = "ltr";
console.log(document.dir);  // "ltr"
Try It Yourself

How It Works

  1. Assign "rtl". The document becomes right-to-left.
  2. Read back. document.dir reports the new value.
  3. Assign "ltr". Switch back to left-to-right.
  4. Use for language toggles. Common when a site offers LTR and RTL locales.

Example 3 — Compare with <html dir>

See how the Document property relates to the root element attribute.

JavaScript
console.log(document.dir);
console.log(document.documentElement.getAttribute("dir"));

document.dir = "rtl";
console.log(document.documentElement.getAttribute("dir"));
Try It Yourself

How It Works

  1. Read document.dir. Document-level API value.
  2. Read the attribute. May be null if markup never set dir.
  3. Assign via JS. Setting document.dir typically updates the root dir.
  4. Pick one approach. Markup for fixed locales; JS for runtime language switches.

Browser Support

Document.dir is Baseline Widely available (MDN: since March 2017). Safe to use for reading and setting document directionality.

✓ Baseline · Widely available

Document.dir

String property for document text direction — "ltr" or "rtl".

98% Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
dir Excellent

Bottom line: Safe for production. Prefer

in markup when the locale is fixed at load time; use document.dir for runtime language / direction switches.

Frequently Asked Questions

A string for the document’s text directionality: "ltr" (left to right) or "rtl" (right to left). MDN documents these as the possible values.
No. MDN marks Document.dir as Baseline Widely available (since March 2017). It is not Deprecated, Experimental, or Non-standard.
Yes. Assign "ltr" or "rtl" to change the document direction. That updates the document-level direction used for layout and bidirectional text.
They work together. The HTML dir attribute (often on <html>) expresses direction in markup; document.dir is the Document API for reading and writing that document-level directionality.
No. Set document (or <html>) direction for the page default. Use the dir attribute — or HTMLElement.dir — on specific elements when only a subsection needs a different direction.
The HTML attribute supports auto for heuristic direction. Document.dir’s documented values on MDN are "ltr" and "rtl" — prefer those when setting from JavaScript unless you manage auto in markup.

Next: doctype

Learn the Document property that exposes the DocumentType node.

doctype tutorial →

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.

11 people found this page helpful