How to display double quotes in JavaScript

pan1490 picture pan1490 · Dec 17, 2013 · Viewed 64.6k times · Source

On html page if I give name in double quotes then it is not getting reflected on page. It displays a blank string. I tried with escape() function but that didn't work. So what is the way to display a string in double quotes.

One thing I forgot to mention that I want to display the string in input text box.

Answer

MaGnetas picture MaGnetas · Dec 17, 2013

You have several options:

var str = 'My "String"'; //use single quotes for your string
var str = "My \"String\""; //escape your doublequotes
var str = "My "String""; //use it as html special chars