Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/en-US/docs/Web/JavaScript/Reference/Template_literals #2371

Closed
1 task done
dsletten opened this issue Oct 23, 2019 · 6 comments · Fixed by #5903
Closed
1 task done

/en-US/docs/Web/JavaScript/Reference/Template_literals #2371

dsletten opened this issue Oct 23, 2019 · 6 comments · Fixed by #5903
Assignees
Labels
Content:JS JavaScript docs

Comments

@dsletten
Copy link

Request type

  • New documentation

Details

The String.raw() example lists incorrect output, indicating that the result of this expression: String.rawHi\n${2+3}! is "Hi\n5!". But that is a 5-element string which contradicts the next line of the example:
str.length;
// 6

The output should instead be "Hi\n5!"

Hmm...markdown renders that incorrectly...

"Hi\\n5!"

The examples on this page are likewise incorrect: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw

This page on String.raw() also uses the term "template string", which appears to be outdated based on the template literal documentation above.

@Elchi3 Elchi3 removed their assignment Jan 6, 2021
@jadjoubran
Copy link
Contributor

I cannot find the issue in the live page.. am I missing anything?

@dsletten
Copy link
Author

Both issues are still present.
The first page (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) shows this example under the section "Raw strings":

let str = String.raw`Hi\n${2+3}!`;
// "Hi\n5!"

str.length;
// 6

Those 2 results contradict each other.

"Hi\n5!"

is a string of length 5.

Whereas the correct output should be:

> let str = String.raw`Hi\n${2+3}!`
undefined
> str.length
6
> str
'Hi\\n5!'

Furthermore, the second page (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) consistently refers to "template strings" in the text and the link at the bottom of the page. As the first page indicates, this is an obsolete term. The documentation should reflect the usage in the standard: template literals

@jadjoubran
Copy link
Contributor

Thanks for getting back @dsletten
There's a comment afterward that says: the character after 'Hi' // is not a newline character, // '\' and 'n' are two characters. which explains why \n shows up as 2 characters instead of 1. Indeed, if you wanted to create it yourself without String.raw, you would have to use \\n to escape it but this is not the case here, so I don't think it makes sense to write the string escaped (considering that we have the explanation in comments). Does that make sense?

In the template strings page, instead of the comment, there's this code: Array.from(str).join(','); which shows how it is indeed made up of 6 characters. Please let me know if I missed something.

As for the 2nd issue, can you tell me where you saw that the term "template string" is obsolete? I could not find that on the template literals page.

Thanks!

@dsletten
Copy link
Author

dsletten commented Feb 15, 2021

Evidently you are referring to the 2nd page: String.raw(). My observation refers to the first page: Template literals. The example shows this result as a literal String:

let str = String.raw`Hi\n${2+3}!`;
// "Hi\n5!"

In JavaScript, that literal value represents a 5-character string. The backslash must be escaped to show that it is a literal '\' and not a newline '\n'.

With regard to the 2nd issue, the "Template literals" reference page opens with this statement:

They were called "template strings" in prior editions of the ES2015 specification.

If the language specification no longer uses the term "template strings", then ideally the MDN documentation should reflect this.

@jadjoubran
Copy link
Contributor

jadjoubran commented Feb 15, 2021

Thanks @dsletten, I've opened a PR to change template strings to template literals. I did leave out templateString the specification still refers to templateString inside template strings.

Regarding the first issue, I will leave it for someone else, as I get your point but considering the clarification under it I think it makes sense. However, I could be wrong so I will leave it for someone else to take a look at

@chrisdavidmills chrisdavidmills transferred this issue from mdn/sprints Feb 15, 2021
@Rumyra Rumyra added needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. Content:JS JavaScript docs labels Jun 7, 2021
@sideshowbarker
Copy link
Collaborator

I’ve opened #5903 with a fix for code examples

@sideshowbarker sideshowbarker self-assigned this Jun 11, 2021
@sideshowbarker sideshowbarker removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jun 11, 2021
Elchi3 pushed a commit that referenced this issue Jun 14, 2021
* Pushed the fix-fixable-flaws button

* Fix code examples in “Template literals” & String.raw() docs

Fixes #2371
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Content:JS JavaScript docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants