Put quotes around a variable string in JavaScript

var text = "http://example.com";

text = "'"+text+"'";

Would attach the single quotes (') to the front and the back of the string.


I think, the best and easy way for you, to put value inside quotes is:

JSON.stringify(variable or value)

var text = "\"http://example.com\""; 

Whatever your text, to wrap it with ", you need to put them and escape inner ones with \. Above will result in:

"http://example.com"

You can add these single quotes with template literals:

var text = "http://example.com"
var quoteText = `'${text}'`

console.log(quoteText)

Docs are here. Browsers that support template literals listed here.