5

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.

6
  • + to concate the string and escape seq to escap the double quote so that it will be printed with string. Commented Jan 28, 2017 at 11:02
  • you also can use '"' + myName + '"'. use single quotes around the double quote, so you don't have to escape the middle double quote Commented Jan 28, 2017 at 11:05
  • 1
    If you're following a tutorial and it uses document.write, stop now. It will undoubtedly be woefully outdated: the uses for document.write are almost nil in 2017. Commented Jan 28, 2017 at 11:05
  • @lonesomeday That's not true, I use it all the time in JSFiddle when I'm too lazy to press F12. Commented Jan 28, 2017 at 13:05
  • @CoryG But document.write is three characters longer than console.log, so you're actually costing yourself effort. (And it's not as good -- it's only useful for numbers and strings.) Commented Jan 28, 2017 at 13:23

6 Answers 6

14

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

2 Comments

That is really heplful. ' " ' is just equivalent to "\"" and then I need to concate it to the string by +
Yes, exactly. Also works the other way, if you want to print a single quote mark you can surround it by doble ones " ' ". Although is a better practice to just use one type of quote mark in your code (it looks cleaner), you can use both.
2

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

Comments

2

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.

Comments

1

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"

Comments

0

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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.