Reputation: 35
I need double quotes of following variable
var foo='bar';
alert(foo);
normally I did alert('foo')
but I get foo
...
but i need the answer like this "bar" how it is possible in javascript. problem is i am putting ""
around the variable it get foo
var foo = 'bar';
alert("foo");
Upvotes: 1
Views: 75
Reputation: 5396
When you use 'x'
, x
can't be '
character (I mean the basic usage) and the same thing, if you have "x"
, x
can't be "
.
Soulutions:
1- Using single-quote inside double-quote:
console.log("'x'");
2-
You can also print a character by its Unicode, ' Unicode is \u0027
console.log('\u0027\u0027'.toString());
3- escape apostrophes by \
console.log('I\'m JavaScript console.log');
Upvotes: 0
Reputation: 5466
You need to escape "
and alert:
Example snippet:
var foo = 'bar';
alert("\"" + foo + "\"");
Upvotes: 2