diff --git a/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/673263df0eb568a7b450f2fc.md b/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/673263df0eb568a7b450f2fc.md index ef4309ff7d4..bc1f6409eda 100644 --- a/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/673263df0eb568a7b450f2fc.md +++ b/curriculum/challenges/english/blocks/lecture-working-with-strings-in-javascript/673263df0eb568a7b450f2fc.md @@ -5,7 +5,7 @@ challengeType: 19 dashedName: how-do-you-create-a-newline-in-strings-and-escape-strings --- -# --description-- +# --interactive-- When working with strings in JavaScript, there are times when you need to include special characters that the JavaScript engine might otherwise misinterpret. @@ -15,19 +15,14 @@ In many programming languages, including JavaScript, you can create a newline in For example, if you want to break a string into multiple lines, you would use `\n` where you want the new line to begin: +:::interactive_editor + ```js let poem = "Roses are red,\nViolets are blue,\nJavaScript is fun,\nAnd so are you."; console.log(poem); ``` -The output will be: - -```md -Roses are red, -Violets are blue, -JavaScript is fun, -And so are you. -``` +::: The `\n` escape sequence tells JavaScript to insert a line break at that point, which results in the string being displayed across multiple lines. @@ -45,22 +40,30 @@ JavaScript gets confused because it thinks the string ends after the word `"said To fix this, you can escape the inner quotes by placing a backslash (`\`) before them: +:::interactive_editor + ```js let statement = "She said, \"Hello!\""; -console.log(statement); // Output: She said, "Hello!" +console.log(statement); // She said, "Hello!" ``` +::: + The backslash tells JavaScript to treat the quotes as literal characters, so they appear correctly in the output. You can also escape other special characters, such as the backslash itself (`\\`), or single quotes within a string surrounded by single quotes (`\'`). Here's another example using single quotes: +:::interactive_editor + ```js let quote = 'It\'s a beautiful day!'; -console.log(quote); // Output: It's a beautiful day! +console.log(quote); // It's a beautiful day! ``` +::: + By escaping the single quote with `\'`, JavaScript knows to include it as part of the string rather than ending the string early. Escaping and creating newlines are essential when you’re formatting output or handling special characters in strings. These techniques help you prevent errors and ensure your text appears exactly as intended.