How to add double quotes in javascript string?


I was wondering how we can add double quotes in a string in javascript, as I have variable, which can be string of string, like this

var str = 'Hello, "World","Something", Else';

Thanks


Asked by:- neena
0
: 98 At:- 11/21/2022 3:09:15 PM
Javascript double quotes in string







1 Answers
profileImage Answered by:- vikas_jk

Here are the few possible ways to add double quotes in string in javascript

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

document.write(str + "<br/>");
document.write(str2+ "<br/>");
document.write(str3+ "<br/>");

This will have output as below

Your "String"
Your "String"
Your "String"

OR

If you need to add single quotes

var text = "Your String"
var quoteText = `'${text}'`

console.log(quoteText)

In the above code, we are using template literals.

1
At:- 11/21/2022 3:14:32 PM
Works as needed, thanks 0
By : neena - at :- 11/21/2022 3:16:37 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use