13. JavaScript Special Characters and Guidelines

JavaScript Special Characters And Guidelines

Insert Special Characters
Javascript Is Case Sensitive
White Space
Break up a code Line

you ca add special characters to a text string by using the back-slash character.

Insert Special Characters

The backslash (\) is used to insert aposrophes,new lines,quotes, and other special characters into a text string.

var txt="we are the so-called "vikings" from the north";
document.write(txt);
result: we are the so-called

To solve this problem, you must place a backslash(\) before each double quote in "vikings".
THis turns each double quote into a string.

var txt="we are the so-called \"vikings\" from the north";
document.write(txt);
result:we are the so-called "vikings" from the north

document.write("you \& i are singing");
result: you & i are singing!

code outputs
\' single quote
\" double quote
\& ampersand
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed

Javascript is case sensitive

White space

Javascript ignors extra spaces.
you can add white space to your script to make it more readable.

name="Hege";
name = "Hege";

Break up a code line

Text in code statements contained within quotes is called "string literal".
A string literal may not be broken across lines except by inserting the backslash character(\) at the point where you want to break the string:

document.write("Hello \
World");

following will generate "unterminated string literal" script error

document.write("Hello
World");

Another option is to use the concatenate operation(+) to break the string:

document.write("Hello"+
"world!");

code statements may be broken across lines, but the backslash character must not be used in this case.

document.write
("Hello"
+
"world!"
);

break code statements or string litterals across lines only when the length of the line or literal make it difficult to read.

you cannot break up a code line like this

document.write \
("Hello World");

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams