Last Updated : Jan 1, 2023
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.
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> <html> <head> <title>ESCAPE SPECIAL CHARACTERS</title> </head> <body> <div id="res"></div> <script> 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>"; document.getElementById('res').innerHTML=txt; </script> </body> </html>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined one div element with id ‘res’ for appends result on webpage by innerHTML.
- 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’.
- 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’.
- Finally we appended our multi line of text result on webpage div element ‘res’ by innerHTML.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
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.