I need help with escaping literal quotes

Tell us what’s happening:

hey
Your code so far


var myStr = "I am a/'Double quoted\"string inside\"double quotes\".";


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/escaping-literal-quotes-in-strings

I do not Know what exactly is going on here what is it that i have wrong and what is the process to go about it

I see a forward slash (/) and a single quote here ...am a/'Double qu...

In javascript the forward slash has a different meaning :slight_smile:

As @Marmiz mentioned, you’re using a forward slash. To escape characters you use a backslash (\) in JavaScript.

const myStr = "I am a \"double quoted string inside double quotes\".";

You don’t need to escape single-quotes in a double-quote string. Nor vice versa. You just need to escape the same quote style as you’re using to define the string. And, with double quotes, certain special characters if applicable.

1 Like

thanks so i must use the back slash

thank you and the same quote style would be the beginning variable right?

Same quote style, as in:

// Double-quoted string
const doubleQuote = "As a double-quoted string, \"double quotes\" inside me need to be escaped, but single-quotes, like in 'you're', are okay alone.";

// Single-quoted string
const singleQuote = 'And vice versa... in a "single quote string", like you\'re going to need to escape single quote characters, like I just did in the word "you\'re", but I can use double quotes freely.';

im sorry im still very much confused to all of this

In JS you have two ways to define a string:

  • '' single quotes
  • "" double quotes.

example:

let s1 = 'I am a valid string';
let s2 = "I am a valid sting too"

Notie that you cannot mix them If you start with a single quote, it has to finish with a single quote as well, otherwise the JS interpreter cannot understand that the string is terminated:

let s = 'I end with a double"
// SyntaxError: Unterminated string constant

That said, if you want to use a the same quote inside the string as punctuation, you need to escape it, otherwise the JS interpreter don’t have a way to tell it’s punctuation opposed to end of the string

let z = 'It's a miracle'

// SyntaxError: Unexpected token

This happens because JS thinks the string ends at it's... (a single quote)
Same for double quotes.

Hope this helps :+1:

1 Like

Hi,

you can use string literal or template literal for any string like below:
Enclose string inside tilt(`) symbol instead of quotes. This makes string look clean and neat and solves many problems.
let z = ` It’s a miracle `;