-3
myInt = parseInt(myString, 10);
    document.write("\"" + myString +
        "\" when converted to an integer equals " + myInt + "<br/>");

Having trouble getting a full understanding of the backslash escape character. For example, the above code without printing the value of myString with quotes around it could be written as (to my understanding):

 myInt = parseInt(myString, 10);
    document.write( myString +
        " when converted to an integer equals " + myInt + "<br/>");

Ok, so, as shown in the first code example, to add literal quotes you would add \" to each side of ' + myString + '. But why the pair of quotes around ' + myString + ', within the two \" escape characters. Why is it not enough to just add the two backslash characters around myString? Why must there be another pair of quotes within that?

Perhaps, as an example, if someone could show how to add literal quotes to the value of myInt, just as was done to myString? I couldn't get that right after several tries.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Alfonso Giron
  • 279
  • 4
  • 11

2 Answers2

0

It sounds like you confuse something, an escape character is to escape a symbol that otherwise would be interpreted by whatever language you use. With your first expression you will printout:

"3" when converted to an integer equals 3

The escaped quotation marks will print "real" quotation marks around your variable once it is printed.

If you want quotes around myInt, you add \" infront and after it:

myInt = parseInt(myString, 10);
document.write( myString +
    " when converted to an integer equals \"" + myInt + "\"<br/>");

And because you want to print the literal symbol " you have to put it in two " that start and end the string.

Alexander
  • 1,969
  • 19
  • 29
0

But why the pair of quotes around ' + myString + ', within the two \" escape character

You have this:

"something" + myString + "something"

A string literal, concatenated with a string variable, concatenated with another string literal.

It just happens that the last character in the first string literal and the first character in the second string literal are escaped quote characters, because the goal is to place a quote character immediately before and after the value in the variable.

You need quotes around a string literal because that is how a string literal is expressed in JavaScript (and most other programming languages).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335