Computer >> Computer tutorials >  >> Programming >> Javascript

How important is backslash in breaking a string in JavaScript?


Backslash(\) is very important in breaking a string in javascript. Without backslash when a string is breached, it can't be read by javascript there by no output is produced

In the following example since a backslash is not used while breaking a string, no output is displayed.

Example

<html>
<body>
<p id="backslash"></p>
<script>
   document.getElementById("backslash").innerHTML = "Javascript is   // string breached here
   not java!";
</script>
</body>
</html>

In case if the backslash is used then javascript tries to read the entire string and display it in the output as shown below. 

Example

<html>
<body>
<p id="backslash"></p>
<script>
document.getElementById("backslash").innerHTML = "Javascript is \
not java!";
</script>
</body>
</html>

Output

Javascript is not java!