AP_1984
AP_1984

Reputation: 81

Adding double quotes in Javascript

I would like to understand the logic behind that code:

var myName = "John";

document.write("\""+ myName +"\"");

I got what I wanted, ie broswer displayed "John" with double quotes around, but I don't uderstand why I had to use + before and after the string and why escape seq. had to be used in such manner.

Upvotes: 5

Views: 79439

Answers (6)

Horia Coman
Horia Coman

Reputation: 8781

You could have achieved the same with JSON.stringify. Or perhaps "'" + myName + "'", without the escapes.


The reason you need to do what you did is because the quotes you use when specifying "John" are not actually part of the string. They're a little bit of syntax telling the compiler that whatever is inside them is a string value you specify as a literal (as opposed to reading from the user, or from some other data source). But, the value itself is just John, without the quotes. When you print it, JavaScript will just print it as John. You need to add the quotes or any extra decoration yourself.

Upvotes: 2

Oriol
Oriol

Reputation: 288610

Your code is vulnerable to string injection! What if myName contains a quote?

Instead, you should use JSON.stringify

var myName = 'John"abc';
'"' + myName + '"';     // "John"abc"
JSON.stringify(myName); // "John\"abc"

You may want to escape U+2028 and U+2029 too.

Upvotes: 14

Viktor
Viktor

Reputation: 480

In javascript strings are characters wrapped in either single or double quotes. If you in the string want to use a actual single/double quote you have to "escape" it. This is done with the backslash character. A way around it is to use different wrapping quotes from what you use inside the string. See below

document.write("\"" + myName + "\""); // escaped

document.write('" + myName + "'); // not escaped

The plus character is used to concatenate the string. In your example, quote plus variable plus quote which executes to "John"

Upvotes: 1

Samuel Toh
Samuel Toh

Reputation: 19308

The escape character \ acts like a hint to the compiler.

By specifying the escape the compiler knows that the following " character is a string and it is not suppose to be part of your programming code.

E.g. The compiler will see the string "helloWor\"ld" as "helloWorld".

The + operator acts as a string concatenation operator. That is, the compiler will attempt to join the 2 strings into one. E.g. "he" + getString() + "llo" will give you heXXXllo, assuming the function getString() returns you the value XXX

Upvotes: 2

Yogesh G
Yogesh G

Reputation: 1160

Please refer to some basic book or w3schools for basics of JS.

var myName = "John";

Here myName has a value of John without the quotes. the quotes are JS way of recognising any string. Any text within quotes is recognised as String in JS.

Now when you want to display quotes also, you need to make quotes a part of string

So "\"" is basically covering (") around quotes. In JS (+) plus sign is use to concatenate two strings.

Hope this clarifies your question!!

Upvotes: 0

Jorge Anzola
Jorge Anzola

Reputation: 1235

The plus sign is used to concate the strings and the backslash is used to tell JS "hey, this is not the ending doble quote mark, I want the actual sign". You can also use both doble and single quote mark in your favor.

var myName = 'John';
document.write('"' + myName + '"');

Js Fiddle

Upvotes: 6

Related Questions