Giter Club home page Giter Club logo

Comments (6)

jadjoubran avatar jadjoubran commented on May 21, 2024

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

from content.

dsletten avatar dsletten commented on May 21, 2024

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

from content.

jadjoubran avatar jadjoubran commented on May 21, 2024

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!

from content.

dsletten avatar dsletten commented on May 21, 2024

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.

from content.

jadjoubran avatar jadjoubran commented on May 21, 2024

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

from content.

sideshowbarker avatar sideshowbarker commented on May 21, 2024

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

from content.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.