04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Language Basics<br />

Character Literals<br />

The String data type includes several character literals to represent nonprintable or otherwise useful<br />

characters, as listed in the following table:<br />

Literal<br />

Meaning<br />

\n New line<br />

\t Tab<br />

\b Backspace<br />

\r Carriage return<br />

\f Form feed<br />

\\ Backslash (\)<br />

\’ Single quote (‘) — used when the string is delineated by single quotes.<br />

Example: ‘ He said, \’hey.\’’.<br />

\” Double quote (“) – used when the string is delineated by double quotes.<br />

Example: “ He said, \”hey.\””.<br />

\xnn<br />

\unnnn<br />

A character represented by hexadecimal code nn (where n is an octal digit 0 - F).<br />

Example: \x41 is equivalent to “A”.<br />

A Unicode character represented by the hexadecimal code nnnn (where n is a<br />

hexadecimal digit 0 - F). Example: \u03a3 is equivalent to the Greek character .<br />

These character literals can be included anywhere with a string and will be interpreted as if they were a<br />

single character, as shown here:<br />

var text = “This is the letter sigma: \u03a3.”;<br />

In this example, the variable text is 28 characters long even though the escape sequence is six characters<br />

long. The entire escape sequence represents a single character, so it is counted as such.<br />

The length of any string can be returned by using the length property as follows:<br />

alert(text.length); //outputs 28<br />

This property returns the number of 16 - bit characters in the string. If a string contains double - byte<br />

characters, the length property may not accurately return the number of characters in the string.<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!