Skip to content

String

The String global object converts any value to a string primitive when it’s called as a function, and its prototype holds the methods that every string can call. The primitive itself is the string type, and a method call such as "js".toUpperCase() reaches String.prototype through a temporary wrapper object:

Language: JavaScript
> String(42)
'42'

> "realjs".toUpperCase()
'REALJS'

> typeof new String("hi")
'object'

String is an ECMAScript built-in. Literal syntax, typeof, coercion rules, and how .length counts UTF-16 code units are covered in the string entry.

Syntax

The String function takes one optional value to convert:

Language: JavaScript Syntax
String([<value>])
new String([<value>])
Parameter Description
value An optional value of any type to convert. It defaults to the empty string "" when omitted

Without the new keyword, the call returns a string primitive. With new, it returns a String object that wraps the primitive. The new keyword is optional, but it changes the result. Calling String() on a symbol returns a string such as "Symbol(id)", while new String() throws a TypeError for one.

Key Static Methods and Properties

Member Kind Description
String.fromCharCode() Static method Returns a string built from UTF-16 code units
String.fromCodePoint() Static method Returns a string built from Unicode code points, including those above 0xFFFF
String.raw() Static method Returns the raw text of a template literal, with escape sequences left unprocessed

Key Instance Methods and Properties

Member Kind Description
.length Instance property Returns the number of UTF-16 code units in the string
.at() Instance method Returns a one-character string holding the UTF-16 code unit at an index, counting from the end for negative indexes (ES2022)
.includes() Instance method Checks whether the string contains a substring
.startsWith(), .endsWith() Instance method Check whether the string begins or ends with a substring
.indexOf(), .lastIndexOf() Instance method Return the position of a substring, or -1 when it’s missing
.slice() Instance method Returns a section of the string, with negative indexes counting from the end
.split() Instance method Returns an array of substrings divided at a separator
.replace() Instance method Returns a new string with the first match of a pattern replaced, or every match for a global regular expression
.replaceAll() Instance method Returns a new string with every match of a pattern replaced (ES2021)
.match(), .matchAll() Instance method Return the matches of a regular expression
.trim(), .trimStart(), .trimEnd() Instance method Return a new string with whitespace removed from one or both ends
.padStart(), .padEnd() Instance method Return a new string padded to a target length
.toUpperCase(), .toLowerCase() Instance method Return a new string with the case converted
.repeat() Instance method Returns the string repeated a given number of times
.normalize() Instance method Returns the Unicode normalization form of the string
.localeCompare() Instance method Compares two strings in locale-aware sort order
.isWellFormed(), .toWellFormed() Instance method Check for or replace lone surrogates (ES2024)

Strings are immutable, so every method returns a new value and leaves the original unchanged. .isWellFormed() and .toWellFormed() shipped in all major browsers in October 2023 and are now Baseline Widely available.

Examples

A signup form receives a username with stray spaces and mixed case. Chaining .trim() and .toLowerCase() cleans it up before storage:

Language: JavaScript
> "  Ada.Lovelace  ".trim().toLowerCase()
'ada.lovelace'

An invoice system needs fixed-width order numbers. The .padStart() method fills the front with zeros:

Language: JavaScript
> String(7).padStart(5, "0")
'00007'

An upload handler checks a file’s extension and grabs its last character. The .endsWith() and .at() methods do both without index arithmetic:

Language: JavaScript
> const fileName = "report.pdf";
undefined

> fileName.endsWith(".pdf")
true
> fileName.at(-1)
'f'

A Windows path in a template literal would turn \t and \n into a tab and a newline. Tagging the literal with String.raw keeps the backslashes:

Language: JavaScript
> String.raw`C:\temp\new`
'C:\\temp\\new'

Common Gotchas

Wrapper objects created with new String() compare by reference and are always truthy, even when they wrap an empty string:

Language: JavaScript
> new String("a") === new String("a")
false

> Boolean(new String(""))
true

A string pattern passed to .replace() replaces only the first match:

Language: JavaScript
> "2026-11-04".replace("-", "/")
'2026/11-04'

> "2026-11-04".replaceAll("-", "/")
'2026/11/04'

The .replaceAll() method, or a regular expression with the g flag, replaces every match.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 7, 2026