jsguides

Reference

String Methods

Methods available on every JavaScript String instance.

  1. String.fromCodePoint()

    String.fromCodePoint() builds strings from Unicode code points, correctly handling values above U+FFFF. Includes ASCII, emoji, and comparison to fromCharCode().

  2. String.prototype.at()

    String.prototype.at() returns the character at an index with negative-index support, making tail-end access cleaner than manual length arithmetic.

  3. String.prototype.charAt()

    String.prototype.charAt() retrieves the character at a given index in a string. Covers syntax, bracket notation comparison, and Unicode caveats.

  4. String.prototype.charCodeAt()

    String.prototype.charCodeAt() returns an integer for the UTF-16 code unit at a specified position in a string, useful for character validation and sorting.

  5. String.prototype.codePointAt()

    String.prototype.codePointAt() returns the Unicode code point at a given position, handling surrogate pairs that charCodeAt() would misrepresent.

  6. String.prototype.concat()

    String.prototype.concat() joins strings into one value in JavaScript. Covers variadic arguments, type coercion, and why template literals are a better option.

  7. String.prototype.endsWith()

    Use String.prototype.endsWith() to check if a string ends with a substring. Covers syntax, edge cases, and file-extension patterns in JavaScript.

  8. String.prototype.includes()

    String.prototype.includes() tests whether a string contains a substring, returning a boolean. An ES6 alternative to indexOf with case-sensitive matching.

  9. String.prototype.indexOf()

    String.prototype.indexOf() searches a string and returns the position of a substring or -1. Covers case sensitivity, edge cases, and comparison with includes().

  10. String.prototype.isWellFormed()

    String.prototype.isWellFormed() returns true if a string contains no lone UTF-16 surrogates, false otherwise. Added in ES2024.

  11. String.prototype.lastIndexOf()

    String.prototype.lastIndexOf() searches a string from the end, returning the index of the last matching substring or -1 when not found.

  12. String.prototype.localeCompare()

    String.prototype.localeCompare() performs locale-aware string comparison, returning a number that indicates sort order for building multilingual applications.

  13. String.prototype.match()

    String.prototype.match() searches strings using regex in JavaScript. Returns match arrays, handles the global flag, and supports capture and named groups.

  14. String.prototype.matchAll()

    String.prototype.matchAll() returns an iterator of regex matches with capture groups, named groups, and positions — all using the global flag in JavaScript.

  15. String.prototype.normalize()

    String.prototype.normalize() returns a Unicode normalization form of a string for comparing text with different character compositions that looks identical.

  16. String.prototype.padEnd()

    String.prototype.padEnd() pads a string to a target length by appending characters to the right. Used for fixed-width column alignment in tables and CLI output.

  17. String.prototype.padStart()

    String.prototype.padStart() pads a string to a target length from the left, ideal for fixed-width alignment. Covers syntax, examples, polyfill, and edge cases.

  18. String.prototype.repeat()

    String.prototype.repeat() returns a new string with the original repeated count times. Handles count limits, edge cases, and common patterns.

  19. String.prototype.replace()

    String.prototype.replace() returns a new string with the first match of a pattern swapped. Supports plain strings, regex, and dynamic callback functions.

  20. String.prototype.replaceAll()

    String.prototype.replaceAll() replaces all occurrences of a substring or pattern in a string, returning a new string with every match substituted.

  21. String.prototype.search()

    String.prototype.search() accepts a regex pattern, searches a string for the first match, and returns its index — or -1 when no match is found.

  22. String.prototype.slice()

    String.prototype.slice() extracts a substring between start and end indices without mutating the original. Negative indices count from the string end.

  23. String.prototype.split()

    String.prototype.split() divides a string into an array of substrings using a separator. Supports string and regex patterns with an optional limit argument.

  24. String.prototype.startsWith()

    The String.prototype.startsWith() checks if a string begins with a specified substring, optionally from a given position. Returns true or false, case-sensitive.

  25. String.prototype.substring()

    Use String.prototype.substring() to extract a portion of a string between two indices. Covers argument swapping, edge cases, and comparison with slice().

  26. String.prototype.toLowerCase()

    String.prototype.toLowerCase() returns a new lowercase string for case-insensitive comparisons and input normalization, leaving the original unchanged.

  27. String.prototype.toUpperCase()

    String.prototype.toUpperCase() returns a new uppercase string for case-insensitive comparisons, constant generation, and locale-aware text processing.

  28. String.prototype.toWellFormed()

    Returns a new string with all lone UTF-16 surrogates replaced by the Unicode replacement character (U+FFFD), so the result is well-formed UTF-16.

  29. String.prototype.trim()

    String.prototype.trim() removes whitespace from both ends of a string, returning a new, cleaned copy while leaving the original unchanged.

  30. String.prototype.trimEnd()

    String.prototype.trimEnd() removes trailing whitespace from a string without modifying the original. Learn syntax, use cases, and how it differs from trim().

  31. String.prototype.trimStart()

    trimStart() removes leading whitespace from a string, returning a new string — ideal for cleaning user input, log entries, and template literal indentation.