> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/str/unicode/encode/utf16/surrogate-pair.md).

# surrogate pair

two-code-unit character.

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [primitive](/web/js/val/prim.md) ⟩ [String](/web/js/val/prim/str.md) ⟩ [Unicode](/web/js/val/prim/str/unicode.md) ⟩ [encoding](/web/js/val/prim/str/unicode/encode.md) ⟩ [UTF-16](/web/js/val/prim/str/unicode/encode/utf16.md) ⟩ surrogate pair

{% hint style="success" %}
[**code points**](/web/js/val/prim/str/unicode/code-point.md) beyond the [**basic multilingual plane**](/web/js/val/prim/str/unicode/code-plane.md) (also called <mark style="color:purple;">**astral code points**</mark>) that are transformed into <mark style="color:yellow;">**two UTF-16**</mark> [**code units**](/web/js/val/prim/str/unicode/encode/utf16/code-unit.md).

* two parts of the pair must be in <mark style="color:yellow;">**`0xD800`**</mark> \~ <mark style="color:yellow;">**`0xDFFF`**</mark>. (2^11 = 0x800)
  * <mark style="color:purple;">**high-surrogate**</mark> code unit :  <mark style="color:yellow;">**0xD800**</mark> \~ <mark style="color:yellow;">**0xDBFF**</mark>. (2^10 = 0x400)
  * <mark style="color:purple;">**low-surrogate**</mark> code unit : <mark style="color:yellow;">**0xDC00**</mark> \~ <mark style="color:yellow;">**0xDFFF**</mark>. (2^10 = 0x400)
* these <mark style="color:orange;">**code units**</mark> (called "<mark style="color:orange;">**lone surrogates**</mark>") <mark style="color:yellow;">**are**</mark>**&#x20;**<mark style="color:red;">**not**</mark>**&#x20;**<mark style="color:yellow;">**used**</mark> to encode single-code-unit characters.&#x20;
  {% endhint %}

{% tabs %}
{% tab title="🗺️ 圖表" %} <img src="https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2FeAPkVT4OexgWTMyevBZY%2Fcode.point.svg?alt=media&amp;token=c612329d-0a48-4cfc-9949-764991d69905" alt="" class="gitbook-drawing">
{% endtab %}

{% tab title="📗" %}

* [ ] Eloquent JavaScript ⟩ [Strings & Character Codes](https://eloquentjavascript.net/05_higher_order.html#h_gQf5HZNGpM)
  {% endtab %}

{% tab title="📘 手冊" %}

* [UTF-16 characters, code points, grapheme clusters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_characters_unicode_codepoints_and_grapheme_clusters)
* String.prototype ⟩&#x20;
  * [.charCodeAt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt)
  * [.codePointAt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt)
* [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) ⟩&#x20;
  * [.fromCodePoint()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)
  * [.fromCharCode()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode)
    {% endtab %}
    {% endtabs %}
