The String
global object is a constructor for strings or a sequence of characters.
String literals take the forms:
'string text' "string text" "中文 español Deutsch English देवनागरी العربية português বাংলা русский 日本語 ਪੰਜਾਬੀ 한국어 தமிழ் עברית"
Strings can also be created using the String
global object directly:
String(thing)
thing
Starting with ECMAScript 2015, string literals can also be so-called Template literals:
`hello world` `hello! world!` `hello ${who}` tag `<a>${who}</a>`
Besides regular, printable characters, special characters can be encoded using escape notation:
Code | Output |
---|---|
\XXX | an octal Latin-1 character. |
\' | single quote |
\" | double quote |
\\ | backslash |
\n | new line |
\r | carriage return |
\v | vertical tab |
\t | tab |
\b | backspace |
\f | form feed |
\uXXXX | unicode codepoint |
\u{X} ... \u{XXXXXX}
| unicode codepoint |
\xXX | the Latin-1 character |
Unlike some other languages, JavaScript makes no distinction between single-quoted strings and double-quoted strings; therefore, the escape sequences above work in strings created with either single or double quotes.
Sometimes, your code will include strings which are very long. Rather than having lines that go on endlessly, or wrap at the whim of your editor, you may wish to specifically break the string into multiple lines in the source code without affecting the actual string contents. There are two ways you can do this.
You can use the + operator to append multiple strings together, like this:
let longString = "This is a very long string which needs " + "to wrap across multiple lines because " + "otherwise my code is unreadable.";
Or you can use the backslash character ("\") at the end of each line to indicate that the string will continue on the next line. Make sure there is no space or any other character after the backslash (except for a line break), or as an indent; otherwise it will not work. That form looks like this:
let longString = "This is a very long string which needs \ to wrap across multiple lines because \ otherwise my code is unreadable.";
Both of these result in identical strings being created.
Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length
, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf()
method, or extracting substrings with the substring()
method.
There are two ways to access an individual character in a string. The first is the charAt()
method:
return 'cat'.charAt(1); // returns "a"
The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:
return 'cat'[1]; // returns "a"
For character access using bracket notation, attempting to delete or assign a value to these properties will not succeed. The properties involved are neither writable nor configurable. (See Object.defineProperty()
for more information.)
C developers have the strcmp()
function for comparing strings. In JavaScript, you just use the less-than and greater-than operators:
var a = 'a'; var b = 'b'; if (a < b) { // true console.log(a + ' is less than ' + b); } else if (a > b) { console.log(a + ' is greater than ' + b); } else { console.log(a + ' and ' + b + ' are equal.'); }
A similar result can be achieved using the localeCompare()
method inherited by String
instances.
String
objectsNote that JavaScript distinguishes between String
objects and primitive string values. (The same is true of Boolean
and Numbers
.)
String literals (denoted by double or single quotes) and strings returned from String
calls in a non-constructor context (i.e., without using the new
keyword) are primitive strings. JavaScript automatically converts primitives to String
objects, so that it's possible to use String
object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.
var s_prim = 'foo'; var s_obj = new String(s_prim); console.log(typeof s_prim); // Logs "string" console.log(typeof s_obj); // Logs "object"
String primitives and String
objects also give different results when using eval()
. Primitives passed to eval
are treated as source code; String
objects are treated as all other objects are, by returning the object. For example:
var s1 = '2 + 2'; // creates a string primitive var s2 = new String('2 + 2'); // creates a String object console.log(eval(s1)); // returns the number 4 console.log(eval(s2)); // returns the string "2 + 2"
For these reasons, the code may break when it encounters String
objects when it expects a primitive string instead, although generally, authors need not worry about the distinction.
A String
object can always be converted to its primitive counterpart with the valueOf()
method.
console.log(eval(s2.valueOf())); // returns the number 4
StringView
— a C-like representation of strings based on typed arrays.String.prototype
String
object.String.fromCharCode()
String.fromCodePoint()
String.raw()
String
generic methodsString generics are non-standard, deprecated and will get removed in the near future.
The String
instance methods are also available in Firefox as of JavaScript 1.6 (not part of the ECMAScript standard) on the String
object for applying String
methods to any object:
var num = 15; console.log(String.replace(num, "5", "2"));
For migrating away from String generics, see also Warning: String.x is deprecated; use String.prototype.x instead.
Generics are also available on Array
methods.
String
instancesString.prototype.constructor
String.prototype.length
N
length
. These properties are read-only.String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.includes()
String.prototype.endsWith()
String.prototype.indexOf()
String
object of the first occurrence of the specified value, or -1 if not found.String.prototype.lastIndexOf()
String
object of the last occurrence of the specified value, or -1 if not found.String.prototype.localeCompare()
String.prototype.match()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.quote()
"
").String.prototype.repeat()
String.prototype.replace()
String.prototype.search()
String.prototype.slice()
String.prototype.split()
String
object into an array of strings by separating the string into substrings.String.prototype.startsWith()
String.prototype.substr()
String.prototype.substring()
String.prototype.toLocaleLowerCase()
toLowerCase()
.String.prototype.toLocaleUpperCase()
toUpperCase()
.String.prototype.toLowerCase()
String.prototype.toSource()
Object.prototype.toSource()
method.String.prototype.toString()
Object.prototype.toString()
method.String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimStart()
String.prototype.trimLeft()
String.prototype.trimEnd()
String.prototype.trimRight()
String.prototype.valueOf()
Object.prototype.valueOf()
method.String.prototype[@@iterator]()
Iterator
object that iterates over the code points of a String value, returning each code point as a String value.These methods are of limited use, as they provide only a subset of the available HTML tags and attributes.
String.prototype.anchor()
<a name="name">
(hypertext target)String.prototype.big()
<big>
String.prototype.blink()
<blink>
String.prototype.bold()
<b>
String.prototype.fixed()
<tt>
String.prototype.fontcolor()
<font color="color">
String.prototype.fontsize()
<font size="size">
String.prototype.italics()
<i>
String.prototype.link()
<a href="url">
(link to URL)String.prototype.small()
<small>
String.prototype.strike()
<strike>
String.prototype.sub()
<sub>
String.prototype.sup()
<sup>
It's possible to use String
as a more reliable toString()
alternative, as it works when used on null
, undefined
, and on symbols
. For example:
var outputStrings = []; for (var i = 0, n = inputValues.length; i < n; ++i) { outputStrings.push(String(inputValues[i])); }
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) The definition of 'String' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'String' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes | Yes | Yes |
@@iterator |
Yes | Yes | 36
|
No | No | No |
Unicode code point escapes \u{xxxxxx} | Yes | ? | 40 | ? | Yes | Yes |
anchor
|
Yes | Yes | 1
|
No | Yes | Yes |
big
|
Yes | Yes | 1 | Yes | Yes | Yes |
blink
|
Yes | Yes | 1 | Yes | Yes | Yes |
bold
|
Yes | Yes | 1 | Yes | Yes | Yes |
charAt |
Yes | Yes | 1 | Yes | Yes | Yes |
charCodeAt |
Yes | Yes | 1 | Yes | Yes | Yes |
codePointAt |
41 | Yes | 29 | No | 28 | 10 |
concat |
Yes | Yes | 1 | Yes | Yes | Yes |
endsWith |
41 | Yes | 17 | No | 28 | 9 |
fixed
|
Yes | Yes | 1 | Yes | Yes | Yes |
fontcolor
|
Yes | Yes | 1 | Yes | Yes | Yes |
fontsize
|
Yes | Yes | 1 | Yes | Yes | Yes |
fromCharCode |
Yes | Yes | 1 | Yes | Yes | Yes |
fromCodePoint |
41 | Yes | 29 | No | 28 | 10 |
includes |
41 | Yes | 40
|
No | Yes | 9 |
indexOf |
Yes | Yes | 1 | Yes | Yes | Yes |
italics
|
Yes | Yes | 1 | Yes | Yes | Yes |
lastIndexOf |
Yes | Yes | 1 | 6 | Yes | Yes |
length |
Yes | Yes | 1 | Yes | Yes | Yes |
link
|
Yes | Yes | 1 | Yes | Yes | Yes |
localeCompare |
Yes | Yes | 1 | Yes | Yes | Yes |
localeCompare.locales |
24 | Yes | 29 | 11 | 15 | 10 |
localeCompare.options |
24 | Yes | 29 | 11 | 15 | 10 |
match |
Yes | Yes | 1 | Yes | Yes | Yes |
match.flags
|
No | No | 1 — 49 | No | No | No |
normalize |
34 | Yes | 31 | No | Yes | 10 |
padEnd |
57 | 15 | 48 | No | 44 | 10 |
padStart |
57 | 15 | 48 | No | 44 | 10 |
prototype |
Yes | Yes | 1 | Yes | Yes | Yes |
quote
|
No | No | 1 — 37 | No | No | No |
raw |
41 | Yes | 34 | No | No | 10 |
repeat |
41 | Yes | 24 | No | Yes | 9 |
replace |
Yes | Yes | 1 | Yes | Yes | Yes |
replace.flags
|
No | No | 1 — 49 | No | No | No |
search |
Yes | Yes | 1 | Yes | Yes | Yes |
search.flags
|
No | No | 1 — 49 | No | No | No |
slice |
Yes | Yes | 1 | Yes | Yes | Yes |
small
|
Yes | Yes | 1 | Yes | Yes | Yes |
split |
Yes | Yes | 1 | Yes | Yes | Yes |
startsWith |
41 | Yes | 17 | No | 28 | 9 |
strike
|
Yes | Yes | 1 | Yes | Yes | Yes |
sub
|
Yes | Yes | 1 | Yes | Yes | Yes |
substr
|
Yes | Yes | 1 | Yes | Yes | Yes |
substring |
Yes | Yes | 1 | Yes | Yes | Yes |
sup
|
Yes | Yes | 1 | Yes | Yes | Yes |
toLocaleLowerCase |
Yes | Yes | 1 | Yes | Yes | Yes |
toLocaleLowerCase.locale |
? | ? | 55 | ? | ? | ? |
toLocaleUpperCase |
Yes | Yes | 1 | Yes | Yes | Yes |
toLocaleUpperCase.locale |
? | ? | 55 | ? | ? | ? |
toLowerCase |
Yes | Yes | 1 | Yes | Yes | Yes |
toSource
|
No | No | 1 | No | No | No |
toString |
Yes | Yes | 1 | Yes | Yes | Yes |
toUpperCase |
Yes | Yes | 1 | Yes | Yes | Yes |
trim |
Yes | Yes | 3.5 | 9 | 10.5 | 5 |
trimEnd |
66
|
? | 61
|
No | 53 | ? |
trimStart |
66
|
? | 61
|
No | 53 | ? |
valueOf |
Yes | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
@@iterator |
No | No | ? | 36
|
No | No | No |
Unicode code point escapes \u{xxxxxx} | Yes | Yes | ? | 40 | Yes | Yes | Yes |
anchor
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
big
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
blink
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
bold
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
charAt |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
charCodeAt |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
codePointAt |
Yes | Yes | Yes | 29 | Yes | 10 | Yes |
concat |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
endsWith |
Yes | 36 | Yes | 17 | Yes | 9 | Yes |
fixed
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
fontcolor
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
fontsize
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
fromCharCode |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
fromCodePoint |
Yes | Yes | Yes | 29 | Yes | 10 | Yes |
includes |
Yes | Yes | Yes | 40
|
Yes | 9 | Yes |
indexOf |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
italics
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
lastIndexOf |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
length |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
link
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
localeCompare |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
localeCompare.locales |
No | 26 | ? | No | No | 10 | Yes |
localeCompare.options |
No | 26 | ? | No | No | 10 | Yes |
match |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
match.flags
|
No | No | No | 4 — 49 | No | No | No |
normalize |
No | 34 | Yes | 31 | Yes | 10 | Yes |
padEnd |
57 | 57 | Yes | 48 | 44 | 10 | 7.0 |
padStart |
57 | 57 | Yes | 48 | 44 | 10 | 7.0 |
prototype |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
quote
|
No | No | No | 4 — 37 | No | No | No |
raw |
No | 41 | Yes | 34 | No | 10 | 4.0 |
repeat |
No | 36 | Yes | 24 | Yes | 9 | Yes |
replace |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
replace.flags
|
No | No | No | 4 — 49 | No | No | No |
search |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
search.flags
|
No | No | No | 4 — 49 | No | No | No |
slice |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
small
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
split |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
startsWith |
Yes | 36 | Yes | 17 | Yes | 9 | Yes |
strike
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
sub
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
substr
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
substring |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
sup
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleLowerCase |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleLowerCase.locale |
? | ? | ? | 55 | ? | ? | ? |
toLocaleUpperCase |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleUpperCase.locale |
? | ? | ? | 55 | ? | ? | ? |
toLowerCase |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toSource
|
No | No | No | 4 | No | No | No |
toString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toUpperCase |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
trim |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
trimEnd |
66
|
66
|
? | 61
|
53 | ? | ? |
trimStart |
66
|
66
|
? | 61
|
53 | ? | ? |
valueOf |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
@@iterator |
0.12 |
Unicode code point escapes \u{xxxxxx} | Yes |
anchor
|
Yes |
big
|
Yes |
blink
|
Yes |
bold
|
Yes |
charAt |
Yes |
charCodeAt |
Yes |
codePointAt |
4.0.0
|
concat |
Yes |
endsWith |
4.0.0
|
fixed
|
Yes |
fontcolor
|
Yes |
fontsize
|
Yes |
fromCharCode |
Yes |
fromCodePoint |
4.0.0
|
includes |
4.0.0 |
indexOf |
Yes |
italics
|
Yes |
lastIndexOf |
Yes |
length |
Yes |
link
|
Yes |
localeCompare |
Yes |
localeCompare.locales |
? |
localeCompare.options |
? |
match |
Yes |
match.flags
|
No |
normalize |
0.12 |
padEnd |
8.0.0
|
padStart |
8.0.0
|
prototype |
Yes |
quote
|
No |
raw |
4.0.0 |
repeat |
4.0.0
|
replace |
Yes |
replace.flags
|
No |
search |
Yes |
search.flags
|
No |
slice |
Yes |
small
|
Yes |
split |
Yes |
startsWith |
4.0.0
|
strike
|
Yes |
sub
|
Yes |
substr
|
Yes |
substring |
Yes |
sup
|
Yes |
toLocaleLowerCase |
Yes |
toLocaleLowerCase.locale |
? |
toLocaleUpperCase |
Yes |
toLocaleUpperCase.locale |
? |
toLowerCase |
Yes |
toSource
|
No |
toString |
Yes |
toUpperCase |
Yes |
trim |
Yes |
trimEnd |
10.0.0 |
trimStart |
10.0.0 |
valueOf |
Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String