Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double quote in JavaScript string

How can I include a double quote in a JavaScript string to be shown in the browser?

I am working on my JavaScript homework and I have to include double quotes in the middle of my list as shown below:

if (i == 0) {     error +=  "<li> this is not the name "....." </li>\n" } 
like image 799
user1318393 Avatar asked Apr 07 '12 15:04

user1318393


Video Answer


2 Answers

Use single quotes.

error += '<li> this is not the name "....." </li>\n'; 

Or escape the double quotes.

error += "<li> this is not the name \".....\" </li>\n"; 
like image 150
Alex Avatar answered Oct 06 '22 17:10

Alex


Use single quotes as your string delimiters:

if (i == 0) {    error += '<li> this is not the name "....." </li>\n' } 

If you have single quotes in the string, delimit it with double quotes.

If you have double quotes in the string, delimit it with single quotes.

If you need to use both types in the string, escape whatever delimiter you have chosen by prefixing it with a \.

like image 38
Oded Avatar answered Oct 06 '22 17:10

Oded