Search code examples
javascriptecmascript-6ecmascript-2017ecma

ECMAScript 2017: Difference between string literal, StringValue, String value, and SV


The below excerpts refer to ECMAScript 2017.

11.8.4.2 Static Semantics: StringValue

StringLiteral::
    "DoubleStringCharactersopt"
    'SingleStringCharactersopt'

1. Return the String value whose elements are the SV of this StringLiteral.

11.8.4.3 Static Semantics: SV

A string literal stands for a value of the String type. The String value (SV) of the literal is described in terms of code unit values contributed by the various parts of the string literal.

Questions

In the excerpts above, the following terms appear:

  1. string literal
  2. Nonterminal symbol StringLiteral
  3. String value
  4. SV

Could someone help explain the difference between these terms?

Also, what does the last sentence in 11.8.4.2 mean?


Solution

  • A string literal is the thing that you, a human writing or reading code, can recognize as the sequence "..." or '...'

    The token StringLiteral is a nonterminal in the formal grammar of EMCAScript that can be replaced by a terminal that is an actual string literal.

    A string value is the semantic content of a string literal. The spec says

    The String value (SV) of the literal is ...

    Therefore, we may be sure that a string literal has a string value: the string value of some string literal is a collection of code unit values.

    The identifier SV appears to be shorthand for (and used interchangeably with) "string value".


    Also, what does the last sentence in 11.8.4.2 mean?

    Every nonterminal "returns" some value when it is evaluated. The line

    Return the String value whose elements are the SV of this StringLiteral.

    simply means that when the parser finds a StringLiteral in the text of a program, the result of parsing that nonterminal is the string value (i.e., collection of code unit values) associated with the just-parsed StringLiteral.