208_man
208_man

Reputation: 1728

How to add single quote "'" to javascript string

So many related questions out there, but none satisfyingly answered using javascript (no jQuery).

I wish to add quotes around a variable in a string I'm building. My string itself should contain single quotes within it like so:

'{'a'}'

I can get: '{a}'

When I try to add single quotes around the key a I get:

'{\'a\'}'

I've used both of the following syntax with the same result. Any suggestions??

concat('\'','a','\'')

'\''+'a'+'\''

See line 39 of this code: https://repl.it/@mike_butak/LiveForHisPleasureHeReallyIsThere

Thanks!

Upvotes: 0

Views: 5208

Answers (2)

Peter Barrett Bryan
Peter Barrett Bryan

Reputation: 675

Having a hard time replicating your issue! See:

   var temp = '\'{\'a\'}\'';
   console.log('s' + temp + 's');

I'd definitely recommend demonstrating the issue you are asking about in a console print or in a readily available editor online before posting a question!

As per your comment, updating to make it part of a variable assignment. Still unclear what the issue is!

Upvotes: 1

user3483203
user3483203

Reputation: 51185

Like this?

console.log("'{'a'}'")

To expand on this, when you are building the string, just use " around the string, and ' within the string.

Upvotes: 1

Related Questions