Using Quotation Marks in Strings

By: Dusty Arlia
Published on August 9, 2012
Last Updated on Friday, July 17, 2015 at 4:17 PM
Total Updates: 7

When you are creating strings, enclose it in either a pair of single quotation marks or double quotation marks. Do not use one at the beginning and a different one at the end. For example:

"This is wrong'
'This is still wrong"
"This is right"
'This is also correct'

If you need to place a quotation mark in your string, you can either use the opposite type of quotation mark as your enclosing quotation marks or you can escape your quotes using the backslash (\). To escape means to interpret literally, not as code. Different characters need escaping, usually because they're special characters or you need extra whitespace. Here's a list of escape characters. The backslash tells the JavaScript interpreter to treat the quote literally. Doing this incorrectly can lead to JavaScript errors. Here are some examples:

"This is 'ok'"             //example of how to use opposite quotation marks
"And this is \"ok\""       //example of how to escape your quotation marks
'He said, "Don\'t move."'  //example of using both methods

Comments:

Ad: