Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Escaping a character

Hello!
Could someone please explain to me what "escaping a character / value" means for example in HTML or overall?
I've just read this: "By default, React DOM escapes any values embedded in JSX before rendering them." and I don't really know what this means.

Hello Dávid!

An example that might make it more clear to you is this. Imagine creating a string called description with a value of "The dog said: "I went to the living room to find my bone"."

const description = "The dog said: "I went to the living room to find my bone"."

As you can see, there are four " characters. JavaScript would be unable to see where the string ends and would give an error.

To fix this issue, we have to add a / before each " that is part of the text, not the code.

const description = "The dog said: /"I went to the living room to find my bone/"."

Now it will now that the first " is the start of the string. The second and third " have the / before it so it knows that that is part of the text. And it will finally end the string with the final ".

I hope you understand it a bit more now. :)

Ohh so that's it.. Thank you!
And here I thought it's something advanced lvl stuff since I didn't know what it stands for, what a relief :)

1 Answer

Steven Parker
Steven Parker
231,269 Points

The description Benjamin gave is essentially correct, but the standard escape character is not slash ( / ). The actual escape character is back‍slash ( \ :point_left: the one that faces the other way).

Example:

const description = "The dog said: \"I went to the living room to find my bone\".";

Another way to deal with this situation is to use a different kind of quote to enclose the string, then no "escape" is needed:

const description = 'The dog said: "I went to the living room to find my bone".';

Exactly, my bad. :)