* 型 [CODE(JS)@en[String]]、文字列値 (JavaScript)

[1]
>    A [DFN@en[string value]] is a [[member]] of the [[type]] [CODE(JS)@en[[[String]]]] and is
a finite ordered sequence of zero or more [[16-bit unsigned integer values]].
>    '''NOTE'''    Although each [[value]] usually represents a single [[16-bit unit]] of [[UTF-16]] 
[[text]], the language does not place any restrictions or requirements on the [[values]] except that
they be [[16-bit unsigned integers]].

;; [CITE[ECMAScript Language Specification (HTML version)]] ([TIME[2009-04-03 04:43:48 +09:00]] 版) <http://bclary.com/2004/11/07/#a-4.3.16>

[2]
>The [[type]] [CODE(JS)@en[[[String]]]] is the [[set]] of all [[string values]].

;; [CITE[ECMAScript Language Specification (HTML version)]] ([TIME[2009-04-03 04:43:48 +09:00]] 版) <http://bclary.com/2004/11/07/#a-4.3.17>

** Unicode との関係

[5] >>1 の定義と NOTE より、 [[UTF-16]] として不正な値 ([[サロゲート・ペア]]の片割れ) が含まれていたり、
[[Unicode]] の[[文字]]ではない[[符号位置]] ([[非文字]]) や[[文字]]が割り当てられていない[[符号位置]]や
[CODE(char)[[[U+0000]]]] [CODE(charname)@en[[[NULL]]]] が含まれていても [[JavaScript]]
的には構わないことになります。

[6] ただし [[JavaScript]] の[[関数]]すべてで [[UTF-16]] として不適切な値がチェックなしで素通しになるわけではありません。
例えば [CODE(JS)@en[[[encodeURIComponent]]()]] は[[サロゲート・ペア]]の片割れが見つかると[[例外]]を投げます。

[8] また、明記されていませんが [[BOM]] も特別扱いされないことになります。実際上は [[BOM]]
を使わない [[UTF-16]] [[符号単位]]の列として理解・実装されています。 
[[UTF-16符号単位]]が[[文字列]]の構成要素として定義されているので、
定義上[[エンディアン]]問題は [[JavaScript]] [[プログラム]]内には持ち込まれていません。[[文字列]]に内部的に
[[BOM]] を持たせる[[実装]]は理論上あり得ますが、現実にはそのような[[実装]]は複雑になるだけで利点もなく、
存在しないと思います。

** DOM・WebIDL との関係

[7] [[DOM]]・[[WebIDL]] では [CODE(DOMi)@en[[[DOMString]]]] に相当します。
[[JavaScript]] が [[UTF-16]] を採用していた歴史的経緯もあって
[CODE(DOMi)@en[[[DOMString]]]] も [[UTF-16符号単位]]の列とされています。

** 関連

[9] 多くの[[プログラミング言語]]に存在する[[文字列型]]に相当しますが、各言語とは細部で色々と異なります。

[10] [[UTF-16符号単位]]の列である点では [[Java]] の [CODE(Java)@en[[[String]]]] と同じです。

[11] [[C]] の[[文字列]] ([CODE(C)@en[[[char]]]] の[[配列]]) は[[バイト列]]であり、 [[JavaScript]]
とは大きく異なります。

[12] [[Perl]] の[[文字列]]は[[バイト列]]または [[utf8]] [[文字列]]であり、 [[JavaScript]]
とは違いがあります。

[13] [[XPathデータ・モデル]]の [CODE(XPath)@en[[[string]]]] は[[文字列]]であり、 [[JavaScript]]
とは異なります。

* [CODE(JS)@en[String]] オブジェクト (JavaScript)

[3]
>A [DFN[String object]] is a [[member]] of the [[type]] [CODE(JS)@en[[[Object]]]] and is an [[instance]]
of the [[built-in]] [CODE(JS)@en[[[String]]]] [[object]]. That is, a [[String object]] is created by using the
[CODE(JS)@en[[[String]]]] [[constructor]] in a [CODE(JS)@en[[[new]]]] [[expression]], supplying a [[string]] as
an [[argument]]. The resulting [[object]] has an implicit (unnamed) [[property]] that is the [[string]].
A [CODE(JS)@en[[[String]]]] [[object]] can be coerced to a [[string value]] by calling the [CODE(JS)@en[[[String]]]]
[[constructor]] as a [[function]] (15.5.1).

;; [CITE[ECMAScript Language Specification (HTML version)]] ([TIME[2009-04-03 04:43:48 +09:00]] 版) <http://bclary.com/2004/11/07/#a-4.3.18>

* 文字列表記 (JavaScript)

[4]
[CITE[Kazuho@Cybozu Labs: JavaScript の文字列定数でエスケープが必要な文字]] ([TIME[2007-01-12 23:05:51 +09:00]] 版) <http://labs.cybozu.co.jp/blog/kazuho/archives/2006/11/js_string_literal2.php>
([[名無しさん]] [WEAK[2007-01-12 14:14:06 +00:00]])

* 文字列 (JSON)

[15] [[JSON]] における[DFN[[RUBYB[[[文字列]]]@en[string]]]]は、0個[[以上]]の
[[Unicode文字]]の列です。

;; [14] [CITE@en[RFC 4627 - The application/json Media Type for JavaScript Object Notation (JSON)]] ([TIME[2011-08-16 17:02:48 +09:00]] 版) <http://tools.ietf.org/html/rfc4627#section-1>

* メモ

[16] [CITE@en-US[Slimmer and faster JavaScript strings in Firefox | JavaScript]]
([TIME[2015-05-28 14:57:21 +09:00]] 版)
<https://blog.mozilla.org/javascript/2014/07/21/slimmer-and-faster-javascript-strings-in-firefox/>

[18] [CITE[Issue 2217 - v8 - HTML extensions to String.prototype shouldn’t escape ', < and > in argument values; only " - V8 JavaScript Engine - Google Project Hosting]]
( ([TIME[2012-09-09 14:52:16 +09:00]] 版))
<http://code.google.com/p/v8/issues/detail?id=2217>


[17] [CITE@en[Define JavaScript string and scalar value string]]
([[annevk]]著, [TIME[2017-03-27 16:01:49 +09:00]])
<https://github.com/whatwg/infra/commit/f1be763cfba23d2fc780b35403074c599e69616e>

[19] [CITE@en[Define length for byte sequences and strings]]
([[annevk]]著, [TIME[2017-03-27 16:59:15 +09:00]])
<https://github.com/whatwg/infra/commit/c484a4fe0d7bdc52b0f17ab99e49eaa9e56e3f56>

[20] [CITE@en[Editorial: let code point and friends be defined by Infra]]
([[annevk]]著, [TIME[2017-03-29 17:47:15 +09:00]])
<https://github.com/whatwg/html/commit/59595d9c2ccadb9332c15048be5d30174532ee70>

[21] [CITE@en[Remove `String` extensions · whatwg/javascript@b7ade3d]]
([TIME[2016-03-13 21:45:30 +09:00]] 版)
<https://github.com/whatwg/javascript/commit/b7ade3d9ebbb3373aca1303f4f6516c884dc362c>

[22] [CITE@en[Breaking: refactor structured clone into serialize/deserialize]]
([[domenic]]著, [TIME[2017-03-21 06:09:33 +09:00]])
<https://github.com/whatwg/html/commit/97d644c97335956610a31e8ad98d1a388c063e84>

[23] [CITE@en[Clarify the interpretation of string literal tokens]]
([[Ms2ger]]著, [TIME[2019-01-31 00:24:07 +09:00]])
<https://github.com/heycam/webidl/commit/1fa8c43d2dc0ade28b79464b95078c7d030d19b9>

[24] [CITE@en[Defining a ByteString default value using a string literal gives no clear link to the explanation of how the string is converted to ByteString · Issue #141 · heycam/webidl]]
([TIME[2019-06-20 13:20:22 +09:00]])
<https://github.com/heycam/webidl/issues/141>

[25] [CITE@en[Clarify the interpretation of string literal tokens. by Ms2ger · Pull Request #621 · heycam/webidl]]
([TIME[2019-06-20 13:20:38 +09:00]])
<https://github.com/heycam/webidl/pull/621>