Content deleted Content added
→String: As of the current date, the portion that I removed is no longer accurate, and I'm citing the same source on today's date as was previously cited in 2018. |
→String: I added an example of a multiline string using backticks. I also added information and an example about template literals, a special kind of string in JavaScript. |
||
Line 311:
=== String ===
A [[String (computer science)|string]] in JavaScript is a sequence of characters. In JavaScript, strings can be created directly (as literals) by placing the series of characters between double (") or single (') quotes. Such strings must be written on a single line, but may include escaped newline characters (such as \n). The JavaScript standard allows the [[Grave accent#Use in programming|backquote]] character (`, a.k.a. grave accent or backtick) to quote multiline literal strings, as well as template literals, which allow for interpolation of expressions within a string.<ref>{{cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals|title=Template literals|website=MDN Web Docs|language=en-US|access-date=2023-11-04}}</ref>
<syntaxhighlight lang="javascript">
const greeting = "Hello, World!";
const anotherGreeting = 'Greetings, people of Earth.';
const aMultilineGreeting = `Warm regards,
John Doe.`
// Template literals type coerce evaluated expressions and interpolate them into the string.
const templateLiteral = `This is what is stored in anotherGreeting: ${anotherGreeting}.`
</syntaxhighlight>
|