JavaScript Document characterSet Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Document.characterSet is a read-only string: the character encoding the document is currently rendered with (MDN)—commonly UTF-8.

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

Syntax

Use document.characterSet. Start with this line.

JavaScript
document.characterSet
CallWhat it does
document.characterSetPrimary signature.

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

Library declarations
JavaScript
document.characterSet

Parameters

ParameterDescription
characterSetRequired. Read-only encoding label string
NormalizationOptional. Useful before string equality checks

Return Value

Return ValueDescription
*The current value of document.characterSet.

JavaScript Examples

Example 1 — Log document.characterSet

Print the encoding label the browser used for this document (MDN pattern).

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

How It Works

  1. Read the property. No arguments—just access document.characterSet.
  2. Get a string label. Common values include UTF-8 and ISO-8859-1.
  3. Reflects decode time. The value is the encoding used to render the document.
  4. Debug aid. Use it when text looks garbled (mojibake) to confirm encoding.

Example 2 — Verify UTF-8

Normalize the label, then check whether the page is using UTF-8.

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

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

How It Works

  1. Normalize. Uppercase and replace _ with - so labels compare cleanly.
  2. Compare to UTF-8. true means the document is rendered as UTF-8.
  3. Keep the raw value. Log the original string for support reports.
  4. If false. Inspect <meta charset> and HTTP Content-Type.

Example 3 — characterSet vs charset

The legacy alias returns the same string; prefer characterSet in new code.

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

How It Works

  1. Read both names. characterSet is the standard property name.
  2. Alias check. charset is kept for older scripts and equals the same string.
  3. Strict equality. === is true when both report the same label.
  4. Style tip. Write characterSet in new tutorials and production code.

Browser Support

Document.characterSet is Baseline Widely available on MDN (since July 2015). It works across modern browsers and long-supported evergreen releases.

✓ Baseline · Widely available

Document.characterSet

Read-only encoding label string for the current document — typically UTF-8 on modern pages.

99% Browser support
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
characterSet Excellent

Bottom line: Safe everywhere you teach DOM APIs. Prefer characterSet over the legacy charset alias. Pair with a correct <meta charset="utf-8"> in every HTML page.

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.
Typically <meta charset="utf-8">, the Content-Type HTTP charset parameter, or a BOM. The browser picks the effective encoding from these signals.
No. It is read-only. Fix encoding with the HTML meta tag, server headers, or by saving the file correctly — not via JavaScript assignment.

Next: childElementCount

Learn how to count element children on a document or element node.

childElementCount 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