JavaScript Tutorial: String Variables
[this page | pdf | back links]
JavaScript
strings consist of a series of consecutive characters, e.g.
var x = "Cat";
A string technically
consists of a series (an ‘array’, except that a JavaScript array is a specific
type of variable) of characters, which is zero-indexed. So, if we assigned x the value of "Cat" then x[0]
would be "C", x[1] would be "a", etc.
Strings support the
following properties and methods. Some of these involve regular
expressions.
Properties:
Property
|
Description
|
More
|
constructor
|
Returns object’s
constructor function
|
Here
|
length
|
Returns length of
string
|
Here
|
prototype
|
Allows author to add
properties and methods to an object
|
Here
|
Methods:
Method
|
Description
|
More
|
charAt()
|
Returns the character at specified index position (note strings
in JavaScript are zero index based, so the first character is at position
zero)
|
Here
|
charCodeAt()
|
Returns the Unicode
character code of the character at specified index position (note strings in
JavaScript are zero index based, so the first character is at position zero)
|
Here
|
concat()
|
Returns
the result of joining two or more strings together
|
Here
|
endsWith()
|
Returns true if the string ends with a specified string,
otherwise returns false
|
Here
|
fromCharCode()
|
Returns the string
corresponding to a specified Unicode character
|
Here
|
includes()
|
Returns true if the
string contains a specified string, otherwise returns false
|
Here
|
indexOf()
|
Returns the position of the first occurrence of a specified
string in the string
|
Here
|
lastIndexOf()
|
Returns the position of
the last occurrence of a specified string in the string
|
Here
|
localeCompare()
|
Returns a number which is -1 if string is before specified string
in sort order, 0 if they are the same and +1 if string is after specified
string in sort order
|
Here
|
match()
|
Searches for matches
within a string versus a specified regular
expression and returns these as a string array
|
Here
|
repeat()
|
Returns
a string that repeats a specified string a specified number of times
|
Here
|
replace()
|
Searches for matches
within a string versus a specified value (or regular expression) and returns
a string in which these are replaced by another string
|
Here
|
search()
|
Searches
for matches within a string versus a specified value or regular expression
and returns the position of first occurrence of a match (or -1 if there is no
match)
|
Here
|
slice()
|
Returns
a new string formed by a part of the original string
|
Here
|
split()
|
Returns an array of substrings that are created by splitting the
original string using a given delimiter
|
Here
|
startsWith()
|
Returns true if the
string starts with a specified string, otherwise returns false
|
Here
|
substr()
|
Returns a substring
defined by the start position and number of characters
|
Here
|
substring()
|
Returns a substring
defined by the start and end position (not including the end position). If
the start position is after the end position then the two are treated as
reversed.
|
Here
|
toLocaleLowerCase()
|
Returns a string that is the original string converted to lower
case characters, bearing in mind the language settings of the browser (so
sometimes does not return the same as toLowerCase)
|
Here
|
toLocaleUpperCase()
|
Returns a string that
is the original string converted to upper case characters, bearing in mind
the language settings of the browser (so sometimes does not return the same
as toUpperCase)
|
Here
|
toLowerCase()
|
Returns a string that
is the original string converted to lower case characters
|
Here
|
toString()
|
Returns the (string)
value of a string
|
Here
|
toUpperCase
|
Returns a string that
is the original string converted to upper case characters
|
Here
|
trim()
|
Returns a string with whitespace (i.e. spaces) removed from
start and finish of string
|
Here
|
valueOf()
|
Returns the primitive
value of an object. For a string, this in effect just returns the string
value of the string
|
Here
|
The Unicode character
code returned by charCodeAt() or
used as input to fromCharCode()
is developed by the Unicode Consortium.
To handle special
characters, you ‘escape’ the character using an escape sequence starting with a
backslash character, typically followed by one of a handful of specially
recognised characters (many of which were originally designed to control
typewriters, so do not make much sense in HTML), or by a Unicode character of
the form u followed by 4 hexadecimal characters. The following table lists a
few examples of such escape sequences.
Unicode character
|
Alternative escape
sequence (if exists)
|
Meaning / comment
|
\u005C
|
\\
|
Backslash, , i.e. \
|
\u0008
|
\b
|
Backspace
|
\u000C
|
\f
|
Form feed
|
\u000A
|
\n
|
Line feed (i.e. new
line)
|
\u000D
|
\r
|
Carriage return
|
\u0009
|
\t
|
Horizontal tab
|
\u000B
|
\v
|
Vertical tab
|
\u0027
|
\'
|
Single quote, i.e. '
|
\u0022
|
\"
|
Double quote, i.e.
"
|
\u0020
|
|
Space
|
\u00A0
|
|
Non-breaking space
|
\u2028
|
|
Line separator
|
\u2029
|
|
Paragraph separator
|
Single quotation marks do
not seem to need to be escaped in a string which is delimited by double
quotation marks and vice-versa, e.g. it is generally possible to define strings
using e.g. the following, without needing to escape the quotation mark
contained in the string.
var x = "a single quotation mark:
'";
var y = 'a double quotation mark: "'
Some further quotation
mark characters are included in the page on the CSS quotes property. Other
types of escaping that work in other HTML or CSS contexts (or even in JavaScript
regular expressions), e.g. using \005C rather than \u005C, do not necessarily
work consistently or at all in JavaScript. So, for cross-browser compatibility
it is usually desirable to use either the short escape sequence as above (if it
exists) or a full Unicode escape sequence.
NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables