Widely availableSupported across all major browsers. Safe to use in production.

Overview

The string type (and String object) represents a sequence of characters.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
1
12
1
1
18
1

The [Symbol.iterator]() method of String values implements the iterable protocol and allows strings to be consumed by most syntaxes expecting iterables, such as the spread syntax and Statements/for...of loops. It returns a string iterator object that yields the Unicode code points of the string value as individual strings.

38
12
36
9
38
9

The String() constructor creates String objects. When called as a function, it returns primitive values of type String.

1
12
1
1
18
1

The charAt() method of String values returns a new string consisting of the single UTF-16 code unit at the given index.

1
12
1
1
18
1

The charCodeAt() method of String values returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

1
12
1
1
18
1

The concat() method of String values concatenates the string arguments to this string and returns a new string.

1
12
1
1
18
1

The String.fromCharCode() static method returns a string created from the specified sequence of UTF-16 code units.

1
12
1
1
18
1

The indexOf() method of String values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.

1
12
1
1
18
1

The lastIndexOf() method of String values searches this string and returns the index of the last occurrence of the specified substring. It takes an optional starting position and returns the last occurrence of the specified substring at an index less than or equal to the specified number.

1
12
1
1
18
1

The length data property of a String value contains the length of the string in UTF-16 code units.

1
12
1
1
18
1

The match() method of String values retrieves the result of matching this string against a regular expression.

1
12
1
1
18
1

The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.

1
12
1
1
18
1

The search() method of String values executes a search for a match between a regular expression and this string, returning the index of the first match in the string.

1
12
1
1
18
1

The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.

1
12
1
1
18
1

The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

1
12
1
1
18
1

The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.

1
12
1
1
18
1

The toLowerCase() method of String values returns this string converted to lower case.

1
12
1
1
18
1

The toString() method of String values returns this string value.

1
12
1
1
18
1

The toUpperCase() method of String values returns this string converted to uppercase.

1
12
1
1
18
1

The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string.

4
12
3.5
5
18
5
String.unicode code point escapes

Unicode code point escapes \u{xxxxxx}

1
12
40
1
18
1

The valueOf() method of String values returns this string value.

1
12
1
1
18
1
Other

A string literal is zero or more Unicode code points enclosed in single or double quotes. Unicode code points may also be represented by an escape sequence. All code points may appear literally in a string literal except for these code points:

1
12
1
1
18
1
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 6 item(s)
Removed
  • This feature was removed in a later browser version (36)
  • This feature was removed in a later browser version (27)
Implementation note
  • Previously available under a different name: @@iterator (27)
  • A placeholder property named `@@iterator` is used.
  • Previously available under a different name: iterator (17)
  • A placeholder property named `iterator` is used.

Syntax

JAVASCRIPT
const str = 'Hello, World!';

str.charAt(0);        // 'H'
str.indexOf('World'); // 7
str.slice(0, 5);      // 'Hello'
str.split(', ');      // ['Hello', 'World!']
str.toLowerCase();    // 'hello, world!'

Live demo

String. split and join

by. Character in connect.. with Array to split, join. with split

PreviewFullscreen

partialstring. Extract

Slice Use, string. specificrange getout..

PreviewFullscreen

Search and index

IndexOf Use, specific. Character that th to exists or..

PreviewFullscreen

Use cases

  • Using String (initial support)

    The string type (and String object) represents a sequence of characters.

Cautions

  • No specific concerns. Stable across all major browsers.

Accessibility

  • When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.

Powered by web-features