What string methods do you know?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Medium
#JavaScript #Strings #JS Basics

Short answer

In JavaScript, strings have a rich set of built-in methods for searching, extracting, transforming, and comparing text. The most popular: length, charAt, charCodeAt, at, concat, includes, indexOf, lastIndexOf, startsWith, endsWith, slice, substring, substr, toLowerCase, toUpperCase, trim, trimStart, trimEnd, replace, replaceAll, split, repeat.

In detail

Strings in JavaScript are immutable — this means that methods do not change the original string but return a new one.


1. Property length

Returns the number of characters in a string.

"Hello".length // 5

A common mistake is expecting that length will return the number of bytes. For Unicode characters (emoji, hieroglyphs), the number of characters and bytes may differ.


2. charAt(index)

Returns the character at the specified index.

"JavaScript".charAt(4) // "S"

If the index is out of range, an empty string is returned.


3. charCodeAt(index)

Returns the numeric code of the character in UTF-16 format.

"A".charCodeAt(0) // 65

4. at(index)

A new method that supports negative indexes.

"Hello".at(-1) // "o"

5. concat(str)

Concatenates strings.

"Hello".concat(" ", "world") // "Hello world"

In practice, the + operator or template literals are more common.


6. includes(substr)

Checks if a substring is contained.

"JavaScript".includes("Script") // true

Case-sensitive.


7. indexOf(substr) / lastIndexOf(substr)

indexOf returns the first position of a substring, lastIndexOf — the last.

"banana".indexOf("a") // 1
"banana".lastIndexOf("a") // 5

8. startsWith(substr) / endsWith(substr)

Checks the start and end of a string.

"Hello".startsWith("He") // true
"Hello".endsWith("lo")   // true

9. slice(start, end)

Extracts a substring by indexes (supports negative indexes).

"Hello".slice(1, 4) // "ell"

10. substring(start, end)

Similar to slice, but does not support negative indexes and swaps arguments if start > end.

"Hello".substring(4, 1) // "ell"

11. substr(start, length)

Extracts a substring by start position and length.

"Hello".substr(1, 3) // "ell"

The method is deprecated, better use slice.


12. toLowerCase() / toUpperCase()

Changes the case of all characters.

"JavaScript".toUpperCase() // "JAVASCRIPT"

13. trim(), trimStart(), trimEnd()

Removes whitespace from the edges of a string.

"  hi  ".trim() // "hi"

14. replace(search, value) / replaceAll(search, value)

replace replaces the first match, replaceAll — all matches.

"1-2-3".replace("-", "_")      // "1_2-3"
"1-2-3".replaceAll("-", "_")   // "1_2_3"

15. split(delimiter)

Splits a string into an array.

"apple,banana".split(",") // ["apple", "banana"]

16. repeat(count)

Repeats a string the specified number of times.

"ha".repeat(3) // "hahaha"

Common mistakes

  1. Expecting methods to modify the string — they always return a new one.
  2. Case sensitivity in search methods (includes, indexOf).
  3. Using the deprecated substr.
  4. Incorrect handling of Unicode — length and charAt may behave unexpectedly with emoji.

💡 Tip: when searching case-insensitively, convert both strings to the same case:

"Hello".toLowerCase().includes("he") // true