JavaScript: 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. JavaScript: Default Charset/Encoding
  2. JavaScript: String is 16-Bit Unit Sequence
  3. JavaScript: Unicode Character Escape Sequence
  4. JavaScript: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML XML Entities

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

  1. JavaScript: String Object
  2. JavaScript: String.prototype
  3. JavaScript: String Constructor
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.