• 0

JS Help - Understanding this code


Question

Hi guys,

I've just started learning Javascript, and I was wondering how you would interpret the bolded section in this code fragment, what is the purpose of the backslash in and around the "color:magenta" line? Does it operate the same way if it were used in Java, i.e. inserting something extra in a string?

<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- Fig. 6.3: welcome2.html -->

<!-- Printing one line with multiple statements. -->

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>

<title>Printing a Line with Multiple Statements</title>

<script type = "text/javascript">

<!--

document.write( "<h1 style = \"color: magenta\">" );

document.write( "Welcome to JavaScript " +

"Programming!</h1>" );

// -->

</script>

</head><body></body>

</html>

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

The backslash is an escape character. That means in this case, the \ escapes the double-quote, and so it renders it as is, rather than parsing it.

Personally, I see that as a terrible example to be used if teaching javascript as anything more than learning what an escape character does.

Link to comment
Share on other sites

  • 0

Would anyone also be able to explain why the '</h1>' tag near the end isn't actually printed but read as a format style?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.