Strings
Strings are enclosed by either single ('
) or double ("
) quotes and can contain zero or more characters:
var empty = ''; var girl_cat = 'Sabine'; var boy_cat = "Dakota"; var zip_code = '06517';
Your string can also contain quotes, but you need to be careful to escape any quotes that match the quotes you are using to enclose your string:
var my_string = 'This "quoted text" is fine'; my_string = "This 'quoted text' is fine"; my_string = 'This string\'s "quote" is escaped'; my_string = "This string's \"quotes\" are escaped';
It can get a little confusing if you don’t maintain some form of consistency. Most JavaScript developers tend to use single quotes to wrap strings . This is likely a holdover from other languages where double-quoted strings are processed differently than single-quoted ones.
Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.