# I need help with escaping literal quotes

**URL:** <https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 19, 2019, 2:06am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747 "2019-06-19T02:06:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jabrilp97](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jabrilp97](https://forum.freecodecamp.org/u/Jabrilp97)\
**Post date:** [June 19, 2019, 2:06am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/1 "2019-06-19T02:06:34Z")

</div>

**Tell us what’s happening:**

hey  
**Your code so far**

```js

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:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/escaping-literal-quotes-in-strings)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![Jabrilp97](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jabrilp97](https://forum.freecodecamp.org/u/Jabrilp97)\
**Post date:** [June 19, 2019, 2:08am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/2 "2019-06-19T02:08:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [June 19, 2019, 2:23am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/3 "2019-06-19T02:23:17Z")

</div>

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

In javascript the forward slash has a different meaning 🙂

---

<div class="post-metadata">

**Author:** ![bhensley](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bhensley/32/117936_2.png) [@bhensley](https://forum.freecodecamp.org/u/bhensley)\
**Post date:** [June 19, 2019, 2:26am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/4 "2019-06-19T02:26:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jabrilp97](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jabrilp97](https://forum.freecodecamp.org/u/Jabrilp97)\
**Post date:** [June 19, 2019, 2:59am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/5 "2019-06-19T02:59:24Z")

</div>

thanks so i must use the back slash

---

<div class="post-metadata">

**Author:** ![Jabrilp97](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jabrilp97](https://forum.freecodecamp.org/u/Jabrilp97)\
**Post date:** [June 19, 2019, 3:00am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/6 "2019-06-19T03:00:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bhensley](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bhensley/32/117936_2.png) [@bhensley](https://forum.freecodecamp.org/u/bhensley)\
**Post date:** [June 19, 2019, 3:06am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/7 "2019-06-19T03:06:21Z")

</div>

Same quote style, as in:

```auto
// 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.';

```

---

<div class="post-metadata">

**Author:** ![Jabrilp97](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jabrilp97](https://forum.freecodecamp.org/u/Jabrilp97)\
**Post date:** [June 19, 2019, 3:22am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/8 "2019-06-19T03:22:09Z")

</div>

im sorry im still very much confused to all of this

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [June 19, 2019, 3:44am UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/9 "2019-06-19T03:44:45Z")

</div>

In JS you have two ways to define a string:

- `''` single quotes
- `""` double quotes.

example:

```auto
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:

```javascript
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**

```auto
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 👍

---

<div class="post-metadata">

**Author:** ![suneethar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suneethar/32/169763_2.png) [@suneethar](https://forum.freecodecamp.org/u/suneethar)\
**Post date:** [July 20, 2020, 12:02pm UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/10 "2020-07-20T12:02:18Z")

</div>

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 `;

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:37pm UTC](https://forum.freecodecamp.org/t/i-need-help-with-escaping-literal-quotes/286747/11 "2021-01-25T12:37:15Z")

</div>


