JavaScript does not have a single function called “all string functions” — instead, it provides a built-in String object with dozens of methods (and a few properties) that let you search, modify, transform, and inspect text. This article lists every standard string method and property defined in ECMAScript 2023, explains what each does with a short example, and covers common pitfalls and practical selection tips.
Background: How JavaScript Strings Work
In JavaScript, a string is a primitive value representing a sequence of UTF-16 code units. When you call a method on a string literal, JavaScript temporarily wraps it in a String object so the method can run. Strings are immutable — every method that appears to change a string actually returns a new string; the original is never modified.
The String object also has a few static methods (called on String itself, not on an instance) and a length property that gives the number of UTF-16 code units. Note that emojis and some accented characters take two code units, so length may not equal the number of visible characters.
Complete List of String Methods and Properties
Here is the full set of standard string functionality in modern JavaScript (ES2023). All methods are available in Node.js and all current browsers unless noted.
length(property) — returns the number of UTF-16 code units.charAt(index)— returns the character at a given position.charCodeAt(index)— returns the UTF-16 code unit (0–65535) at a position.codePointAt(index)— returns the full Unicode code point (handles surrogate pairs).at(index)— returns the character at a position, supports negative indices (ES2022).concat(str1, str2, ...)— joins strings; the+operator is usually faster.includes(searchString, position?)— checks if a substring exists.indexOf(searchString, position?)— returns first index or -1.lastIndexOf(searchString, position?)— returns last index or -1.startsWith(searchString, position?)— tests the beginning.endsWith(searchString, length?)— tests the end.slice(start, end?)— extracts a substring (supports negative indices).substring(start, end?)— extracts a substring (swaps if start > end, no negatives).substr(start, length?)— deprecated, do not use.toUpperCase()— returns an uppercase copy.toLowerCase()— returns a lowercase copy.toLocaleUpperCase(locale?)andtoLocaleLowerCase(locale?)— locale-aware casing.trim()— removes whitespace from both ends.trimStart()/trimEnd()— remove whitespace from one end only.padStart(targetLength, padString?)— pads the beginning.padEnd(targetLength, padString?)— pads the end.repeat(count)— repeats the string.replace(searchValue, replaceValue)— replaces first match (string) or all (regex with /g).replaceAll(searchValue, replaceValue)— replaces all matches (ES2021).split(separator, limit?)— splits into an array.match(regexp)— returns array of matches (or null).matchAll(regexp)— returns iterator of all matches (ES2020).search(regexp)— returns index of first match or -1.localeCompare(otherString, locale?, options?)— compares strings according to locale.normalize(form?)— returns Unicode normalization form (NFC, NFD, etc.).valueOf()andtoString()— return the primitive string.- Static:
String.fromCharCode(...codes)— creates a string from UTF-16 code units. - Static:
String.fromCodePoint(...codePoints)— creates a string from Unicode code points. - Static:
String.raw`template`— returns raw template string (backslashes not escaped).
How to Choose the Right Method
With so many methods, picking the right one depends on what you need:
- Checking existence: use
includes()(boolean),indexOf()(position), orstartsWith()/endsWith()for edges. - Extracting part of a string: use
slice()— it supports negative indices and is more predictable thansubstring(). - Replacing text: use
replace()for a single occurrence,replaceAll()for all. For complex patterns, use a regular expression with thegflag. - Converting to an array: use
split()with a delimiter, orArray.from(str)if you need to handle surrogate pairs properly. - Case conversion: use
toUpperCase()/toLowerCase()for most cases; use locale variants for languages like Turkish where casing rules differ. - Trimming: use
trim()for both ends,trimStart()ortrimEnd()for one side.
Common Problems and Misconceptions
One frequent issue is confusing substring() and slice(). substring() treats negative arguments as 0 and swaps arguments if start > end; slice() accepts negatives and never swaps. Example: "hello".slice(-3) returns "llo", but "hello".substring(-3) returns "hello" because -3 becomes 0.
Another misconception is that replace() replaces all occurrences. It only replaces the first unless you use a global regex: "a-b-c".replace("-", "+") gives "a+b-c". Use replaceAll("-", "+") to get "a+b+c".
Also, length counts UTF-16 code units, so "😀".length is 2. To count visible characters, use Array.from("😀").length or a loop with codePointAt().
Comparing String Methods with Regular Expressions
Many string methods accept either a string or a regular expression. For example, split() and match() behave differently with regexes. "a1b2".split(/\d/) returns ["a", "b", ""] (the delimiter is consumed). "a1b2".match(/\d/g) returns ["1", "2"]. When using regex, be careful with special characters — if you want to split on a literal dot, escape it: "a.b".split(".") splits on any character, so use "a.b".split(/\./).
For simple literal searches, string methods are faster and more readable than regex. For pattern matching (e.g., email validation), regex is the right tool.
Performance and Best Practices
String concatenation with + is fine for small strings, but in loops it can be slow because each concatenation creates a new string. Use an array and join() for many concatenations:
let parts = [];
for (let i = 0; i < 1000; i++) {
parts.push(i.toString());
}
let result = parts.join(",");
For repeated replacements, replaceAll() is usually clearer than a regex. For case-insensitive checks, convert both sides to lowercase once: str.toLowerCase().includes("needle").
When dealing with very long strings, methods like indexOf() are O(n) but fine in practice. Avoid using substring() in hot paths if you need negative indices — use slice().
Frequently Asked Questions
How do I check if a string contains a substring in JavaScript?
Use the includes() method: "Hello world".includes("world") returns true. For case-insensitive, convert both to lowercase: str.toLowerCase().includes("world").
What is the difference between slice, substring, and substr?
slice() and substring() both extract a substring, but slice() supports negative indices and doesn’t swap arguments. substr() is deprecated and uses a start index and length — avoid it. Use slice() for most cases.
How do I replace all occurrences of a character in a string?
Use replaceAll(): "a-b-c".replaceAll("-", "+") returns "a+b+c". Alternatively, use a global regex: "a-b-c".replace(/-/g, "+").
Why does str.length return 2 for an emoji?
Because emojis are represented as surrogate pairs — two UTF-16 code units. To count visible characters, use Array.from(str).length or iterate with codePointAt().
Is there a way to reverse a string in JavaScript?
There is no built-in reverse() for strings, but you can split into an array, reverse, and join: "abc".split("").reverse().join("") gives "cba". For emoji-safe reversal, use Array.from(str).reverse().join("").
What does String.raw do?
String.raw`\n` returns the raw string with backslashes preserved, so String.raw`a\nb` is "a\\nb" (two characters: backslash and n), not a newline. It’s useful for regex patterns and file paths.
Related
- Google Sheets COUNTIF Case Sensitive? Exact Match
- Are Random Number Generators Truly Random?
- PLC Traffic Light Ladder Logic: How It Works
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel Count Nonblank Cells: Formulas and ExamplesTo count nonblank cells in Excel, use =COUNTA(A2:A20), replacing the range with the cells you want to check.…
- Excel GROUPBY With Multiple Criteria: Filter ArraysTo group Excel data while applying multiple row-level criteria, use the filter_array argument in GROUPBY and combine AND…
- DMAX Formula in Excel: Syntax, Examples, and CriteriaExcel’s DMAX function returns the largest numeric value in a database field among records that meet specified criteria.…
- How to Hide Filter Arrows in Excel and Keep Table FiltersTo hide filter arrows in Excel without removing a table’s AutoFilter setup, click inside the table, open Table…
- Excel COUNTIF Examples for Text, Dates, and NumbersExcel’s COUNTIF function counts cells in a range that match one condition. Its syntax is =COUNTIF(range,criteria); for example,…
- Excel Filter Duplicates: Is Matching Case-Sensitive?No. In Excel for Microsoft 365 on Windows, the regular filter and built-in duplicate tools do not distinguish…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.