Sherin AS
Sherin AS

Reputation: 35

how to get double quotes of a variable in javascript

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

Answers (4)

Manoj Gupta
Manoj Gupta

Reputation: 63

Just use this code:

var foo='"bar"';
alert(foo);

Upvotes: 1

Mohammad Kermani
Mohammad Kermani

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

Ajay Narain Mathur
Ajay Narain Mathur

Reputation: 5466

You need to escape " and alert:

Example snippet:

var foo = 'bar';
alert("\"" + foo + "\"");

Upvotes: 2

Kafo
Kafo

Reputation: 3656

Basically you can do this

    var foo='"bar"';
    alert(foo);

Upvotes: 1

Related Questions