recursal/MDN huggingface dataset:
String.prototype.startsWith()
=============================

The **`startsWith()`** method of `String` values determines whether this string begins with the characters of a specified string, returning `true` or `false` as appropriate.

Try it
------

Syntax
------


```js
startsWith(searchString)
startsWith(searchString, position)
```

### Parameters

`searchString`

The characters to be searched for at the start of this string. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passing `undefined` causes `startsWith()` to search for the string `"undefined"`, which is rarely what you want.

`position` Optional

The start position at which `searchString` is expected to be found (the index of `searchString`'s first character). Defaults to `0`.

### Return value

**`true`** if the given characters are found at the beginning of the string, including when `searchString` is an empty string; otherwise, **`false`**.

### Exceptions

`TypeError`

Thrown if `searchString` is a regex.

Description
-----------

This method lets you determine whether or not a string begins with another string. This method is case-sensitive.

Examples
--------

### Using startsWith()


```js
const str = "To be, or not to be, that is the question.";

console.log(str.startsWith("To be")); // true
console.log(str.startsWith("not to be")); // false
console.log(str.startsWith("not to be", 10)); // true
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.startswith |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.startsWith` in `core-js`
* `String.prototype.endsWith()`
* `String.prototype.includes()`
* `String.prototype.indexOf()`
* `String.prototype.lastIndexOf()`
String.prototype.blink()
========================

**Deprecated:** This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The **`blink()`** method of `String` values creates a string that embeds this string in a `<blink>` element (`<blink>str</blink>`), which used to cause a string to blink in old browsers.

**Note:** All HTML wrapper methods are deprecated and only standardized for compatibility purposes. For the case of `blink()`, the `<blink>` element itself is removed from modern browsers, and blinking text is frowned upon by several accessibility standards. Avoid using the element in any way.

Syntax
------


```js
blink()
```

### Parameters

None.

### Return value

A string beginning with a `<blink>` start tag, then the text `str`, and then a `</blink>` end tag.

Examples
--------

### Using blink()

The code below creates an HTML string and then replaces the document's body with it:


```js
const contentString = "Hello, world";

document.body.innerHTML = contentString.blink();
```


This will create the following HTML:


```html
<blink>Hello, world</blink>
```


**Warning:** This markup is invalid, because `blink` is no longer a valid element.

You should avoid blinking elements altogether.

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.blink |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.blink` in `core-js`
* HTML wrapper methods
String.prototype.toWellFormed()
===============================

The **`toWellFormed()`** method of `String` values returns a string where all lone surrogates of this string are replaced with the Unicode replacement character U+FFFD.

Syntax
------


```js
toWellFormed()
```

### Parameters

None.

### Return value

A new string that is a copy of this string, with all lone surrogates replaced with the Unicode replacement character U+FFFD. If `str` is well formed, a new string is still returned (essentially a copy of `str`).

Description
-----------

Strings in JavaScript are UTF-16 encoded. UTF-16 encoding has the concept of *surrogate pairs*, which is introduced in detail in the UTF-16 characters, Unicode code points, and grapheme clusters section.

`toWellFormed()` iterates through the code units of this string, and replaces any lone surrogates with the Unicode replacement character U+FFFD `�`. This ensures that the returned string is well-formed and can be used in functions that expect well-formed strings, such as `encodeURI`. Compared to a custom implementation, `toWellFormed()` is more efficient, as engines can directly access the internal representation of strings.

When ill-formed strings are used in certain contexts, such as `TextEncoder`, they are automatically converted to well-formed strings using the same replacement character. When lone surrogates are rendered, they are also rendered as the replacement character (a diamond with a question mark inside).

Examples
--------

### Using toWellFormed()


```js
const strings = [
  // Lone leading surrogate
  "ab\uD800",
  "ab\uD800c",
  // Lone trailing surrogate
  "\uDFFFab",
  "c\uDFFFab",
  // Well-formed
  "abc",
  "ab\uD83D\uDE04c",
];

for (const str of strings) {
  console.log(str.toWellFormed());
}
// Logs:
// "ab�"
// "ab�c"
// "�ab"
// "c�ab"
// "abc"
// "ab😄c"
```

### Avoiding errors in encodeURI()

`encodeURI` throws an error if the string passed is not well-formed. This can be avoided by using `toWellFormed()` to convert the string to a well-formed string first.


```js
const illFormed = "https://example.com/search?q=\uD800";

try {
  encodeURI(illFormed);
} catch (e) {
  console.log(e); // URIError: URI malformed
}

console.log(encodeURI(illFormed.toWellFormed())); // "https://example.com/search?q=%EF%BF%BD"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.towellformed |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.toWellFormed` in `core-js`
* `String.prototype.isWellFormed()`
* `String.prototype.normalize()`
String.prototype[@@iterator]()
==============================

The **`[@@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 `for...of` loops. It returns a string iterator object that yields the Unicode code points of the string value as individual strings.

Try it
------

Syntax
------


```js
string[Symbol.iterator]()
```

### Parameters

None.

### Return value

A new iterable iterator object that yields the Unicode code points of the string value as individual strings.

Description
-----------

Strings are iterated by Unicode code points. This means grapheme clusters will be split, but surrogate pairs will be preserved.


```js
// "Backhand Index Pointing Right: Dark Skin Tone"
[..."👉🏿"]; // ['👉', '🏿']
// splits into the basic "Backhand Index Pointing Right" emoji and
// the "Dark skin tone" emoji

// "Family: Man, Boy"
[..."👨‍👦"]; // [ '👨', '‍', '👦' ]
// splits into the "Man" and "Boy" emoji, joined by a ZWJ
```

Examples
--------

### Iteration using for...of loop

Note that you seldom need to call this method directly. The existence of the `@@iterator` method makes strings iterable, and iterating syntaxes like the `for...of` loop automatically call this method to obtain the iterator to loop over.


```js
const str = "A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A";

for (const v of str) {
  console.log(v);
}
// "A"
// "\uD835\uDC68"
// "B"
// "\uD835\uDC69"
// "C"
// "\uD835\uDC6A"
```

### Manually hand-rolling the iterator

You may still manually call the `next()` method of the returned iterator object to achieve maximum control over the iteration process.


```js
const str = "A\uD835\uDC68";

const strIter = str[Symbol.iterator]();

console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // "\uD835\uDC68"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype-@@iterator |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype[@@iterator]` in `core-js`
* Text formatting guide
* `Symbol.iterator`
* Iteration protocols
String.prototype.padStart()
===========================

 The **`padStart()`** method of `String` values pads this string with another string (multiple times, if needed) until the resulting
 string reaches the given length. The padding is applied from the start of this string.

Try it
------

Syntax
------


```js
padStart(targetLength)
padStart(targetLength, padString)
```

### Parameters

`targetLength`

 The length of the resulting string once the current `str` has
 been padded. If the value is less than or equal to `str.length`, then
 `str` is returned as-is.


`padString` Optional

 The string to pad the current `str` with. If
 `padString` is too long to stay within the
 `targetLength`, it will be truncated from the end.
 The default value is the unicode "space" character (U+0020).


### Return value

 A `String` of the specified `targetLength` with
 `padString` applied from the start.

Examples
--------

### Basic examples


```js
"abc".padStart(10); // " abc"
"abc".padStart(10, "foo"); // "foofoofabc"
"abc".padStart(6, "123465"); // "123abc"
"abc".padStart(8, "0"); // "00000abc"
"abc".padStart(1); // "abc"
```

### Fixed width string number conversion


```js
// JavaScript version of: (unsigned)
// printf "%0\*d" width num
function leftFillNum(num, targetLength) {
  return num.toString().padStart(targetLength, "0");
}

const num = 123;
console.log(leftFillNum(num, 5)); // "00123"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.padstart |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.padStart` in `core-js`
* `String.prototype.padEnd()`
String.prototype.small()
========================

**Deprecated:** This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The **`small()`** method of `String` values creates a string that embeds this string in a `<small>` element (`<small>str</small>`), which causes this string to be displayed in a small font.

**Note:** All HTML wrapper methods are deprecated and only standardized for compatibility purposes. Use DOM APIs such as `document.createElement()` instead.

Syntax
------


```js
small()
```

### Parameters

None.

### Return value

A string beginning with a `<small>` start tag, then the text `str`, and then a `</small>` end tag.

Examples
--------

### Using small()

The code below creates an HTML string and then replaces the document's body with it:


```js
const contentString = "Hello, world";

document.body.innerHTML = contentString.small();
```


This will create the following HTML:


```html
<small>Hello, world</small>
```


Instead of using `small()` and creating HTML text directly, you should use DOM APIs such as `document.createElement()`. For example:


```js
const contentString = "Hello, world";
const elem = document.createElement("small");
elem.innerText = contentString;
document.body.appendChild(elem);
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.small |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.small` in `core-js`
* HTML wrapper methods
* `<small>`
String.prototype.padEnd()
=========================

 The **`padEnd()`** method of `String` values pads this string with a given
 string (repeated, if needed) so that the resulting string reaches a given length. The
 padding is applied from the end of this string.

Try it
------

Syntax
------


```js
padEnd(targetLength)
padEnd(targetLength, padString)
```

### Parameters

`targetLength`

 The length of the resulting string once the current `str` has
 been padded. If the value is less than or equal to `str.length`, the
 current string will be returned as-is.


`padString` Optional

 The string to pad the current `str` with. If
 `padString` is too long to stay within
 `targetLength`, it will be truncated: for left-to-right
 languages the left-most part and for right-to-left languages the right-most will be
 applied. The default value for this parameter is " "
 (`U+0020`).


### Return value

 A `String` of the specified `targetLength` with the
 `padString` applied at the end of the current
 `str`.

Examples
--------

### Using padEnd


```js
"abc".padEnd(10); // "abc "
"abc".padEnd(10, "foo"); // "abcfoofoof"
"abc".padEnd(6, "123456"); // "abc123"
"abc".padEnd(1); // "abc"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.padend |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.padEnd` in `core-js`
* `String.prototype.padStart()`
String() constructor
====================

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

Syntax
------


```js
new String(thing)
String(thing)
```


**Note:** `String()` can be called with or without `new`, but with different effects. See Return value.

### Parameters

`thing`

Anything to be converted to a string.

### Return value

When `String` is called as a constructor (with `new`), it creates a `String` object, which is **not** a primitive.

When `String` is called as a function, it coerces the parameter to a string primitive. Symbol values would be converted to `"Symbol(description)"`, where `description` is the description of the Symbol, instead of throwing.

**Warning:** You should rarely find yourself using `String` as a constructor.

Examples
--------

### String constructor and String function

String function and String constructor produce different results:


```js
const a = new String("Hello world"); // a === "Hello world" is false
const b = String("Hello world"); // b === "Hello world" is true
a instanceof String; // is true
b instanceof String; // is false
typeof a; // "object"
typeof b; // "string"
```


 Here, the function produces a string (the primitive type) as promised.
 However, the constructor produces an instance of the type String (an object wrapper) and
 that's why you rarely want to use the String constructor at all.

### Using String() to stringify a symbol

`String()` is the only case where a symbol can be converted to a string without throwing, because it's very explicit.


```js
const sym = Symbol("example");
`${sym}`; // TypeError: Cannot convert a Symbol value to a string
"" + sym; // TypeError: Cannot convert a Symbol value to a string
"".concat(sym); // TypeError: Cannot convert a Symbol value to a string
```



```js
const sym = Symbol("example");
String(sym); // "Symbol(example)"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string-constructor |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Text formatting guide
String.prototype.isWellFormed()
===============================

The **`isWellFormed()`** method of `String` values returns a boolean indicating whether this string contains any lone surrogates.

Syntax
------


```js
isWellFormed()
```

### Parameters

None.

### Return value

Returns `true` if this string does not contain any lone surrogates, `false` otherwise.

Description
-----------

Strings in JavaScript are UTF-16 encoded. UTF-16 encoding has the concept of *surrogate pairs*, which is introduced in detail in the UTF-16 characters, Unicode code points, and grapheme clusters section.

`isWellFormed()` allows you to test whether a string is well-formed (i.e. does not contain any lone surrogates). Compared to a custom implementation, `isWellFormed()` is more efficient, as engines can directly access the internal representation of strings. If you need to convert a string to a well-formed string, use the `toWellFormed()` method. `isWellFormed()` allows you to handle ill-formed strings differently from well-formed strings, such as throwing an error or marking it as invalid.

Examples
--------

### Using isWellFormed()


```js
const strings = [
  // Lone leading surrogate
  "ab\uD800",
  "ab\uD800c",
  // Lone trailing surrogate
  "\uDFFFab",
  "c\uDFFFab",
  // Well-formed
  "abc",
  "ab\uD83D\uDE04c",
];

for (const str of strings) {
  console.log(str.isWellFormed());
}
// Logs:
// false
// false
// false
// false
// true
// true
```

### Avoiding errors in encodeURI()

`encodeURI` throws an error if the string passed is not well-formed. This can be avoided by using `isWellFormed()` to test the string before passing it to `encodeURI()`.


```js
const illFormed = "https://example.com/search?q=\uD800";

try {
  encodeURI(illFormed);
} catch (e) {
  console.log(e); // URIError: URI malformed
}

if (illFormed.isWellFormed()) {
  console.log(encodeURI(illFormed));
} else {
  console.warn("Ill-formed strings encountered."); // Ill-formed strings encountered.
}
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.iswellformed |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.isWellFormed` in `core-js`
* `String.prototype.toWellFormed()`
* `String.prototype.normalize()`
String.prototype.sup()
======================

**Deprecated:** This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The **`sup()`** method of `String` values creates a string that embeds this string in a `<sup>` element (`<sup>str</sup>`), which causes this string to be displayed as superscript.

**Note:** All HTML wrapper methods are deprecated and only standardized for compatibility purposes. Use DOM APIs such as `document.createElement()` instead.

Syntax
------


```js
sup()
```

### Parameters

None.

### Return value

A string beginning with a `<sup>` start tag, then the text `str`, and then a `</sup>` end tag.

Examples
--------

### Using sup()

The code below creates an HTML string and then replaces the document's body with it:


```js
const contentString = "Hello, world";

document.body.innerHTML = contentString.sup();
```


This will create the following HTML:


```html
<sup>Hello, world</sup>
```


Instead of using `sup()` and creating HTML text directly, you should use DOM APIs such as `document.createElement()`. For example:


```js
const contentString = "Hello, world";
const elem = document.createElement("sup");
elem.innerText = contentString;
document.body.appendChild(elem);
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.sup |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.sup` in `core-js`
* HTML wrapper methods
* `<sup>`
String.prototype.big()
======================

**Deprecated:** This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The **`big()`** method of `String` values creates a string that embeds this string in a `<big>` element (`<big>str</big>`), which causes this string to be displayed in a big font.

**Note:** All HTML wrapper methods are deprecated and only standardized for compatibility purposes. For the case of `big()`, the `<big>` element itself has been removed from the HTML specification and shouldn't be used anymore. Web developers should use CSS properties instead.

Syntax
------


```js
big()
```

### Parameters

None.

### Return value

A string beginning with a `<big>` start tag, then the text `str`, and then a `</big>` end tag.

Examples
--------

### Using big()

The code below creates an HTML string and then replaces the document's body with it:


```js
const contentString = "Hello, world";

document.body.innerHTML = contentString.big();
```


This will create the following HTML:


```html
<big>Hello, world</big>
```


**Warning:** This markup is invalid, because `big` is no longer a valid element.

Instead of using `big()` and creating HTML text directly, you should use CSS to manipulate fonts. For example, you can manipulate `font-size` through the `element.style` attribute:


```js
document.getElementById("yourElemId").style.fontSize = "2em";
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.big |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.big` in `core-js`
* HTML wrapper methods
* `<big>`
String.prototype.repeat()
=========================

 The **`repeat()`** method of `String` values constructs and returns a new string
 which contains the specified number of copies of this string, concatenated together.

Try it
------

Syntax
------


```js
repeat(count)
```

### Parameters

`count`

 An integer between `0` and
 `+Infinity`, indicating the
 number of times to repeat the string.


### Return value

A new string containing the specified number of copies of the given string.

### Exceptions

`RangeError`

Thrown if `count` is negative or if `count` overflows maximum string length.

Examples
--------

### Using repeat()


```js
"abc".repeat(-1); // RangeError
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc' (count will be converted to integer)
"abc".repeat(1 / 0); // RangeError

({ toString: () => "abc", repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-string.prototype.repeat |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `String.prototype.repeat` in `core-js`
* `String.prototype.concat()`