JS: Default Charset/Encoding

By Xah Lee. Date: . Last updated: .

What is JavaScript's default charset?

What is JavaScript source code's default charset?

It's Unicode.

ECMAScript code is expressed using Unicode, version 8.0.0 or later. ECMAScript source text is a sequence of code points. All Unicode code point values from U+0000 to U+10FFFF, including surrogate code points, may occur in source text where permitted by the ECMAScript grammars. The actual encodings used to store and interchange ECMAScript source text is not relevant to this specification.

ECMAScript® 2016 Language Specification#sec-ecmascript-language-source-code

[see Unicode Basics: What's Character Set, Character Encoding, UTF-8?]

What is JavaScript's Default Encoding?

What is JavaScript source code's default encoding?

JavaScript spec does not specify a default encoding.

[see HTTP Protocol Tutorial]

[see HTML: Character Sets and Encoding]

The actual encodings used to store and interchange ECMAScript source text is not relevant to this specification.

Regardless of the external source text encoding, a conforming ECMAScript implementation processes the source text as if it was an equivalent sequence of SourceCharacter values, each SourceCharacter being a Unicode code point. Conforming ECMAScript implementations are not required to perform any normalization of source text, or behave as though they were performing normalization of source text.

ECMAScript® 2016 Language Specification#sec-ecmascript-language-source-code

The best practice is to use UTF-8 to encode your JavaScript source file, because it's the most widely used on the web and is backward compatible with ASCII.

You might want to start your JavaScript file by this line

// -*- coding: utf-8 -*-

This line indicates what encoding is used for a file, and is standard for Python, Ruby, emacs lisp, and many editors.

[see Python: Unicode Tutorial 🐍]

[see Ruby: Unicode Tutorial 💎]

[see Emacs: Unicode Tutorial]

String Topic

  1. JS: Default Charset/Encoding
  2. JS: String Code Unit vs Code Point
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

  1. JS: Template String
  2. JS: Convert String to Number
  3. JS: Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON Object

  1. JS: String Object
  2. JS: String.prototype
  3. JS: String Constructor
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon