In this above example code, we have printed the string “Hello Welcome” inside the double quotes using the document.write() method. So, we got the string as an output.
In this above example, we print the string without using double quotes. So, it will return the error:
So, we must print the strings inside double quotes. If the string itself contains a double quote character, this can cause issues with the syntax of the code like :
document.write("Every "child" likes an ice cream");
In this case, the double quotes within the string itself are interpreted as the end of the string, causing a syntax error. So, we use the escape character to avoid this issue.
How to escape double quotes?
By using the backslash character ‘/’ to escape the double quotes within the string, we can ensure they are interpreted correctly. For example:
document.write("Every \"child\" likes an ice cream");
In this example, we have printed the double quotes using the backslash escape character. See the code the double quotes are printed successfully.
document.write('Every "child" likes an ice cream');
In this case, the double quotes within the string are interpreted as literal characters, since they are enclosed within backticks rather than single or double quotes.