FANDOM


A string is a sequence of textual characters (like this article). Internally, the characters are represented by numbers.

References ECMA Mozilla Microsoft
Literals [1] [2] [3]
Objects [4] [5][6] [7]


Literal SyntaxEdit

DefinitionEdit

A literal string is defined by enclosing a sequence of characters in either single quotes or double quotes. They are equivalent, but for that single-quoted strings cannot contain an unescaped single quote, and double-quoted strings cannot contain an unescaped double quote. To create a string containing a quotation mark of the type used to delimit the string, the quotation mark must be escaped.

// The following two are equivalent.
"foo"
'foo'
 
// These are illegal and will cause an error:
// "foo"bar"
// 'foo'bar'
 
// But these are valid equivalents.
'foo"bar'
"foo'bar"

Escape SequencesEdit

A string may contain any number of escape sequences. These represent one character each, and are usually used to include special characters in the string that would otherwise break the syntax. They are formed of the backslash character (\) followed by a character or sequence of characters specifying the character to be inserted.

\\ A literal backslash.
\" A literal double quote (even if the string is double-quoted).
\' A literal single quote (even if the string is single-quoted).
\n A line feed (new line).
\r A carriage return.
\t A horizontal tab.
\v A vertical tab.
\b A backspace.
\f A form feed.
\0 A NUL character.
\xNN The Unicode character at the codepoint defined by the hexadecimal number NN.
\uNNNN The Unicode character at the codepoint defined by the hexadecimal number NNNN.

Using the escaped quotation marks, the above invalid examples can be written validly like so:

"foo\"bar"
'foo\'bar'

OperatorsEdit

Combination OperatorsEdit

These operators take two strings and return a new string.

+ (Concatenation)Edit

Creates a new string consisting of the characters of the left operand followed by the characters of the right operand. Beware: if either operand is a string, the other will always be coerced into a string: the string will never be coerced.

"foo" + "bar" // "foobar"
5 + "baz"     // "5baz"
"baz" + 5     // "baz5"
"4" + 5       // "45"
4 + "5"       // "45"

Comparison OperatorsEdit

Comparison operators take two strings and return a boolean value. Strings are compared by their character codes, with earlier characters being weighted higher than later characters.

=== (Equals)Edit

Compares two strings for equality. Values of different types are automatically considered unequal.

"2" === 2       // false
"foo" === "foo" // true

!== (Not Equals)Edit

Compares two values for inequality; x !== y is equivalent to !(x === y).

"foo" !== "bar" // true

> (Greater Than)Edit

Compares two values and returns true if the first is greater than the second.

"b" > "a"   // true
"b" > "b"   // false
"b" > "c"   // false
"bb" > "b"  // true
"bb" > "ba" // true
"bb" > "bc" // false

< (Less Than)Edit

Less-than. Opposite of greater-than. a < b is equivalent to b > a.

"ba" < "bb" // true

>= (Greater Than Or Equal To)Edit

Similar to greater-than, but also returns true if the values are equal: a >= b is equivalent to a > b || a == b.

"bb" >= "ba" // true
"bb" >= "bb" // true
"bb" >= "bc" // false

<= (Less Than Or Equal To)Edit

The opposite of >=. a <= b is equivalent to a < b || a == b.

"bb" <= "ba" // false
"bb" <= "bb" // true
"bb" <= "bc" // true

MembersEdit

These properties and methods are available on the global String constructor.

prototypeEdit

Data type object
Standard ECMA-262 §15.5.3.1
Documentation Mozilla, Microsoft

These members are available on each instance of String. (This includes string literals.)

splitEdit

Data type function
Return type array
Parameter list [separator (string)], limit (number)
Standard ECMA-262 §15.5.4.14
Documentation Mozilla Microsoft

Splits a String object into an array of strings by separating the string into substrings.

separatorEdit

Optional. Specifies the character to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string.

limitEdit

Optional. Integer specifying a limit on the number of splits to be found.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.