All TalkersCode Topics

Follow TalkersCode On Social Media

JavaScript Escape Special Characters

Last Updated : Jan 1, 2023

JavaScript Escape Special Characters

In this tutorial we will show you the solution of JavaScript escape special characters, here we used escape special characters of ‘single quotes, double quotes, back slashes’ in our program to show some example.

Using escape characters to escape a single character or symbol. When using backslash immediately following the backslash is escaped if we use braces to escape an individual character within a word, character is escaped but the word is broken into three tokens.

Step By Step Guide On JavaScript Escape Special Characters :-

Here we defined div element with id’s ‘res’ for append results on webpage.

In script we defined variable ‘txt’ with ‘let’ keyword and it contains multi line of texts with escape character of ‘single quote, double quote, back slash’ to know user how it will work then what is output result when using those escape character in program.

Except we specified escape characters more escape characters available in script with unique usability.

<!DOCTYPE html>
        <title>ESCAPE SPECIAL CHARACTERS</title>
        <div id="res"></div>
            let txt="This line had \'single quotes\' special character<br>This line had \"Double quotes\" special character<br>This line had \back slashes special character<br>";
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined one div element with id ‘res’ for appends result on webpage by innerHTML.
  7. In script variable ‘txt’ defined with ‘let’ keyword for store multi lines of text. There in first line we specified single quote of escape character i.e ‘This line had \'single quotes\' special character’.
  8. Then in another line we specified double quote of escape character i.e ‘This line had \"Double quotes\" special character’ and in last we specified back slash escape character i.e ‘This line had \that back slashes special character’.
  9. Finally we appended our multi line of text result on webpage div element ‘res’ by innerHTML.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to use escape special characters using javascript.

When we executes program on browser we can see the result messages of ‘This line had 'single quotes' special character, This line had "Double quotes" special character, This line had hat back slashes special character’.

We can modify them with other escape special characters then we can see those result also.

I hope this tutorial on JavaScript escape special characters helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials