| JavaScript - Strings | | Print | |
Strings
A JavaScript string is a sequence of arbitrary letters, digits, and other characters from the 16-bit Unicode character set.
String literals appear in JavaScript programs between single or double quotes. One style of quotes may be nested within the other:
'testing'
"3.14"
'name="myform"'
"Wouldn't you prefer O'Reilly's book?"
When the backslash character (\) appears within a string literal, it changes, or escapes, the meaning of the character that follows it. The following table lists these special escape sequences:
|
Escape |
Represents |
|---|---|
|
\b |
Backspace |
|
\f |
Form feed |
|
\n |
Newline |
|
\r |
Carriage return |
|
\t |
Tab |
|
\' |
Apostrophe or single quote that does not terminate the string |
|
\" |
Double-quote that does not terminate the string |
|
\\ |
Single backslash character |
|
\xdd |
Character with Latin-1 encoding specified by two hexadecimal digits dd |
|
\udddd |
Character with Unicode encoding specified by four hexadecimal digits dddd |
The String class defines many methods that you can use to operate on strings. It also defines the length property, which specifies the number of characters in a string.
The addition (+) operator concatenates strings. The equality (==) operator compares two strings to see if they contain exactly the same sequences of characters. (This is compare-by-value, not compare-by-reference, as C, C++, or Java programmers might expect.) The inequality operator (!=) does the reverse. The relational operators(<, <=, >, and >=) compare strings using alphabetical order.
JavaScript strings are immutable, which means that there is no way to change the contents of a string. Methods that operate on strings typically return a modified copy of the string.
| Users' Comments (0) |
|
No comment posted






