JavaScript Document characterSet Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.characterSet is a read-only instance property that returns the character encoding label the browser uses to render the page (such as UTF-8). Learn MDN’s encoding-vs-set distinction, the charset alias, how meta tags affect encoding, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

string label

03

Common

UTF-8

04

Alias

charset

05

Source

meta / HTTP

06

Status

Baseline widely

Introduction

When a browser loads an HTML file, it must decide how to turn bytes into characters. That decision is the document’s character encoding. Modern pages almost always use UTF-8, which supports emoji, accented letters, and scripts from many languages.

document.characterSet lets JavaScript read which encoding label the browser applied—useful for debugging mojibake (garbled text), verifying your <meta charset> tag, or logging locale diagnostics in support tools.

💡
Name vs meaning (MDN)

Despite the property name characterSet, MDN states it returns the encoding, not the abstract character repertoire. A character set and a character encoding are related but not identical.

Related Document tutorials: body, Document constructor, textContent.

Understanding Document.characterSet

A read-only instance property on Document. Its value is a string naming the encoding the document is currently rendered with.

  • Value — encoding label string (e.g. UTF-8, ISO-8859-1).
  • Read-only — you cannot change encoding by assigning to this property.
  • Aliasdocument.charset returns the same value (legacy name).
  • Typical modern valueUTF-8 when <meta charset="utf-8"> is set.
  • Relateddocument.inputEncoding reflects the encoding used to decode the document (often matches).

📝 Syntax

JavaScript
document.characterSet

Value

A string — the document’s character encoding label.

MDN example

JavaScript
console.log(document.characterSet);
// e.g. "UTF-8" or "ISO-8859-1"

⚡ Quick Reference

GoalCode / note
Read encodingdocument.characterSet
Legacy aliasdocument.charset
Check UTF-8document.characterSet.toUpperCase() === "UTF-8"
Log for debuggingconsole.log(document.characterSet)
Change encodingEdit meta tag / HTTP headers (not JS)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.characterSet.

Type
string

Encoding label

Access
read-only

No setter

Typical
UTF-8

Modern web

Status
baseline

Standard API

📋 Character set vs character encoding

TermPlain meaningcharacterSet property
Character setRepertoire of characters (e.g. Unicode)Not what this property returns (MDN)
Character encodingHow bytes map to characters (e.g. UTF-8)Yes — returns encoding label
UTF-8Variable-width Unicode encodingMost common modern value
ISO-8859-1Legacy Western European encodingStill seen on old pages

Examples Gallery

Examples follow MDN Document: characterSet. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the encoding label the browser uses for this document.

Example 1 — Log document.characterSet (MDN)

Print the encoding label to the console or a debug panel.

JavaScript
console.log(document.characterSet);
// "UTF-8" on most modern pages
Try It Yourself

How It Works

The value reflects how the browser decoded the document bytes after considering meta tags and HTTP headers.

Example 2 — Verify UTF-8

Branch when the page is not using UTF-8 (legacy or misconfigured sites).

JavaScript
const isUtf8 =
  document.characterSet.toUpperCase().replace(/_/g, "-") === "UTF-8";

console.log("Using UTF-8:", isUtf8);
console.log("Actual encoding:", document.characterSet);
Try It Yourself

How It Works

Normalize casing and underscores before comparing—browsers may return slightly different label formatting.

📈 Aliases, Meta & Debugging

Legacy alias, HTML declaration, and a small diagnostic helper.

Example 3 — Compare with document.charset

The legacy alias should match characterSet.

JavaScript
console.log(document.characterSet);
console.log(document.charset);
console.log(document.characterSet === document.charset); // true
Try It Yourself

How It Works

Prefer characterSet in new code; keep charset in mind when reading older scripts.

Example 4 — Relate to <meta charset>

Read the meta tag and compare with the live document encoding.

JavaScript
const meta = document.querySelector('meta[charset]');
const declared = meta ? meta.getAttribute("charset") : "(none)";

console.log("meta charset:", declared);
console.log("document.characterSet:", document.characterSet);
Try It Yourself

How It Works

Always declare <meta charset="utf-8"> early in <head>. HTTP headers can override in some cases.

Example 5 — Encoding Debug Panel

Small helper object for support or i18n troubleshooting.

JavaScript
const encodingInfo = {
  characterSet: document.characterSet,
  charset: document.charset,
  inputEncoding: document.inputEncoding,
  lang: document.documentElement.lang || "(not set)"
};

console.log(JSON.stringify(encodingInfo, null, 2));
Try It Yourself

How It Works

Pair encoding with document.documentElement.lang when diagnosing locale or garbled-text bugs.

🚀 Common Use Cases

  • Debugging mojibake — confirm the browser decoded the page as UTF-8.
  • Support tooling — log encoding in bug-report bundles.
  • Legacy site audits — detect ISO-8859-1 pages that need migration.
  • CMS diagnostics — verify meta charset survived template rendering.
  • Not for changing encoding — fix HTML/HTTP instead of JavaScript.
  • i18n education — teach bytes, encoding labels, and Unicode.

🧠 How the Browser Picks an Encoding

1

Bytes arrive

HTML file or HTTP response body as raw bytes.

Network
2

Sniff meta / headers / BOM

Browser determines decoding (often UTF-8 today).

Decode
3

DOM is built

Characters render correctly (or garbled if wrong encoding).

Parse
4

Read with characterSet

JavaScript exposes the chosen encoding label as a read-only string.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only — change encoding via meta tag or HTTP headers, not this property.
  • Despite the name, returns the encoding label (MDN note).
  • document.charset is a legacy alias with the same value.
  • Always use <meta charset="utf-8"> in new HTML documents.
  • Related: body, textContent, Document constructor.

Universal Browser Support

Document.characterSet is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.characterSet

Read-only encoding label string — essential for UTF-8 verification and i18n debugging.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.characterSet Excellent

Bottom line: Use document.characterSet to read how the browser decoded the page. Declare UTF-8 in HTML and fix server headers when text looks wrong — do not try to set encoding from JavaScript.

Conclusion

Document.characterSet is the standard read-only way to learn which character encoding label the browser used to render the page. Use it for diagnostics and legacy audits—and fix encoding at the HTML or HTTP layer, not in JavaScript.

Continue with childElementCount, ownerDocument, body, textContent, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Declare <meta charset="utf-8"> in every HTML page
  • Log document.characterSet when debugging garbled text
  • Use characterSet instead of legacy charset in new code
  • Align HTTP Content-Type charset with your HTML meta tag
  • Pair encoding checks with lang attribute review

❌ Don’t

  • Try to assign a new value to document.characterSet
  • Assume the property name means “Unicode character repertoire”
  • Ship new pages without an explicit UTF-8 declaration
  • Ignore encoding when copying legacy ISO-8859-1 content
  • Rely on JavaScript to fix bytes decoded with the wrong encoding

Key Takeaways

Knowledge Unlocked

Five things to remember about document.characterSet

Read-only encoding label — usually UTF-8 on modern sites.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

read-only

DOM
🔄04

Alias

charset

Legacy
🌐05

Fix via

meta / HTTP

HTML

❓ Frequently Asked Questions

A read-only string naming the character encoding the document is currently rendered with — for example UTF-8 or ISO-8859-1.
No. MDN marks Document.characterSet as Baseline Widely available (since July 2015). It is a standard read-only DOM property.
MDN notes that a character set and a character encoding are related but different. Despite the property name, characterSet returns the encoding label the browser uses to interpret bytes.
Yes. document.charset is a legacy alias that returns the same value as document.characterSet. Prefer characterSet in new code for clarity.
Typically the UTF-8 meta tag (<meta charset="utf-8">), the Content-Type HTTP header charset parameter, or a BOM at the start of the file. The browser picks the effective encoding from these signals.
No. It is read-only. To change encoding, fix your HTML meta tag, server headers, or save the file in the correct encoding — not via JavaScript.
Did you know?

Putting <meta charset="utf-8"> within the first 1024 bytes of your HTML helps the browser detect UTF-8 quickly—before it has to guess. That prevents a flash of incorrectly decoded characters on slow connections.

Next: childElementCount

Learn how many direct element children the document has (usually 1).

childElementCount →

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.

6 people found this page helpful