| Method | Description |
|---|---|
| length | Gets 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 |
lengthReturns the number of characters in the string.
let str = "Hello";
console.log(str.length); // 5
charAt(index)Returns the character at a specified index.
let str = "JavaScript";
console.log(str.charAt(4)); // S
charCodeAt(index)Returns the Unicode of the character at the given index.
let str = "ABC";
console.log(str.charCodeAt(0)); // 65
at(index)Modern alternative to charAt, supports negative indexing.
let str = "Hello";
console.log(str.at(-1)); // o
indexOf(searchValue)Returns the index of the first occurrence.
"hello world".indexOf("world"); // 6
lastIndexOf(searchValue)Returns the index of the last occurrence.
"hello hello".lastIndexOf("hello"); // 6
includes(searchValue)Checks if the string contains the specified value.
"developer".includes("dev"); // true
startsWith(searchValue)Checks if a string starts with a specified value.
"JavaScript".startsWith("Java"); // true
endsWith(searchValue)Checks if a string ends with a specified value.
"hello.js".endsWith(".js"); // true
slice(start, end)Extracts part of a string from start to end (excluding end).
"JavaScript".slice(0, 4); // "Java"
substring(start, end)Similar to slice but doesn't support negative indexes.
"JavaScript".substring(4, 10); // "Script"
substr(start, length) (deprecated)Returns a substring starting at a specified position with a given length.
"JavaScript".substr(0, 4); // "Java"
toUpperCase()Converts the string to uppercase.
"hello".toUpperCase(); // "HELLO"
toLowerCase()Converts the string to lowercase.
"HELLO".toLowerCase(); // "hello"
trim()Removes whitespace from both ends.
" test ".trim(); // "test"
trimStart() / trimEnd()Removes whitespace from start or end.
" test".trimStart(); // "test"
"test ".trimEnd(); // "test"
replace(search, replace)Replaces the first match of a value with another value.
"I like Java".replace("Java", "JS"); // "I like JS"
replaceAll(search, replace)Replaces all occurrences.
"ha ha ha".replaceAll("ha", "ho"); // "ho ho ho"
repeat(n)Repeats the string n times.
"ha".repeat(3); // "hahaha"
concat(string2)Joins strings together.
"Hello".concat(" World"); // "Hello World"
split(separator)Splits the string into an array.
"a,b,c".split(","); // ["a", "b", "c"]
valueOf()Returns the primitive value of a string.
let strObj = new String("hello");
console.log(strObj.valueOf()); // "hello"
toString()Converts a value to a string.
let num = 123;
console.log(num.toString()); // "123"
padStart(length, pad)Pads the start of the string to reach a certain length.
"5".padStart(3, "0"); // "005"
padEnd(length, pad)Pads the end of the string.
"5".padEnd(3, "*"); // "5**"