All JavaScript String Methods

📘 Table of String Methods

Method Description
lengthGets string length
charAt()Gets character at index
charCodeAt()Gets Unicode at index
at()Modern index getter (supports negatives)
indexOf()First occurrence of string
lastIndexOf()Last occurrence of string
includes()Check if string contains
startsWith()Check if starts with
endsWith()Check if ends with
slice()Extract part of string
substring()Part of string (no negative)
substr()Substring by length (deprecated)
toUpperCase()Convert to uppercase
toLowerCase()Convert to lowercase
trim()Remove whitespace ends
trimStart()/trimEnd()Remove from start or end
replace()Replace first match
replaceAll()Replace all matches
repeat()Repeat string
concat()Join strings
split()Split into array
valueOf()Primitive value
toString()Convert to string
padStart()Pad beginning of string
padEnd()Pad end of string

🔤 Basic Properties & Access

1. length

Returns the number of characters in the string.

let str = "Hello";
console.log(str.length); // 5

2. charAt(index)

Returns the character at a specified index.

let str = "JavaScript";
console.log(str.charAt(4)); // S

3. charCodeAt(index)

Returns the Unicode of the character at the given index.

let str = "ABC";
console.log(str.charCodeAt(0)); // 65

4. at(index)

Modern alternative to charAt, supports negative indexing.

let str = "Hello";
console.log(str.at(-1)); // o

🔍 Search & Test

5. indexOf(searchValue)

Returns the index of the first occurrence.

"hello world".indexOf("world"); // 6

6. lastIndexOf(searchValue)

Returns the index of the last occurrence.

"hello hello".lastIndexOf("hello"); // 6

7. includes(searchValue)

Checks if the string contains the specified value.

"developer".includes("dev"); // true

8. startsWith(searchValue)

Checks if a string starts with a specified value.

"JavaScript".startsWith("Java"); // true

9. endsWith(searchValue)

Checks if a string ends with a specified value.

"hello.js".endsWith(".js"); // true

✂️ Extracting Substrings

10. slice(start, end)

Extracts part of a string from start to end (excluding end).

"JavaScript".slice(0, 4); // "Java"

11. substring(start, end)

Similar to slice but doesn't support negative indexes.

"JavaScript".substring(4, 10); // "Script"

12. substr(start, length) (deprecated)

Returns a substring starting at a specified position with a given length.

"JavaScript".substr(0, 4); // "Java"

🔧 Modifying Strings

13. toUpperCase()

Converts the string to uppercase.

"hello".toUpperCase(); // "HELLO"

14. toLowerCase()

Converts the string to lowercase.

"HELLO".toLowerCase(); // "hello"

15. trim()

Removes whitespace from both ends.

"  test  ".trim(); // "test"

16. trimStart() / trimEnd()

Removes whitespace from start or end.

"  test".trimStart(); // "test"
"test  ".trimEnd();   // "test"

17. replace(search, replace)

Replaces the first match of a value with another value.

"I like Java".replace("Java", "JS"); // "I like JS"

18. replaceAll(search, replace)

Replaces all occurrences.

"ha ha ha".replaceAll("ha", "ho"); // "ho ho ho"

19. repeat(n)

Repeats the string n times.

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

🔗 Combination & Conversion

20. concat(string2)

Joins strings together.

"Hello".concat(" World"); // "Hello World"

21. split(separator)

Splits the string into an array.

"a,b,c".split(","); // ["a", "b", "c"]

22. valueOf()

Returns the primitive value of a string.

let strObj = new String("hello");
console.log(strObj.valueOf()); // "hello"

23. toString()

Converts a value to a string.

let num = 123;
console.log(num.toString()); // "123"

24. padStart(length, pad)

Pads the start of the string to reach a certain length.

"5".padStart(3, "0"); // "005"

25. padEnd(length, pad)

Pads the end of the string.

"5".padEnd(3, "*"); // "5**"

🔗 References