-
length - Returns the length of the string.
let str = "hello";
console.log(str.length); // 5
-
toUpperCase() - Converts the string to uppercase.
let str = "hello";
console.log(str.toUpperCase()); // "HELLO"
-
toLowerCase() - Converts the string to lowercase.
let str = "HELLO";
console.log(str.toLowerCase()); // "hello"
-
trim() - Removes whitespace from both ends of the string.
let str = " hello ";
console.log(str.trim()); // "hello"
-
includes() - Checks if the string contains a specified substring.
let str = "hello world";
console.log(str.includes("world")); // true
-
startsWith() - Checks if the string starts with a specified substring.
let str = "hello world";
console.log(str.startsWith("hello")); // true
-
endsWith() - Checks if the string ends with a specified substring.
let str = "hello world";
console.log(str.endsWith("world")); // true
-
slice() - Extracts a section of the string and returns it as a new string.
let str = "hello world";
console.log(str.slice(0, 5)); // "hello"
-
substring() - Similar to slice(), but does not accept negative indices.
let str = "hello world";
console.log(str.substring(0, 5)); // "hello"
-
replace() - Replaces a specified value with another value in the string.
let str = "hello world";
console.log(str.replace("world", "everyone")); // "hello everyone"
-
split() - Splits the string into an array of substrings.
let str = "hello world";
console.log(str.split(" ")); // ["hello", "world"]
-
charAt() - Returns the character at a specified index.
let str = "hello";
console.log(str.charAt(1)); // "e"
-
indexOf() - Returns the index of the first occurrence of a specified value.
let str = "hello world";
console.log(str.indexOf("world")); // 6
-
lastIndexOf() - Returns the index of the last occurrence of a specified value.
let str = "hello world world";
console.log(str.lastIndexOf("world")); // 12
-
concat() - Joins two or more strings.
let str1 = "hello";
let str2 = "world";
console.log(str1.concat(" ", str2)); // "hello world"
-
repeat() - Returns a new string with a specified number of copies of the original string.
let str = "hello";
console.log(str.repeat(3)); // "hellohellohello"
-
padStart() - Pads the string with another string from the start to reach a specified length.
let str = "5";
console.log(str.padStart(3, "0")); // "005"
-
padEnd() - Pads the string with another string from the end to reach a specified length.
let str = "5";
console.log(str.padEnd(3, "0")); // "500"
-
match() - Matches a string against a regular expression.
let str = "hello world";
console.log(str.match(/world/)); // ["world"]
-
search() - Searches the string for a match against a regular expression and returns the index.
let str = "hello world";
console.log(str.search(/world/)); // 6
-
localeCompare() - Compares two strings in the current locale.
let str1 = "apple";
let str2 = "banana";
console.log(str1.localeCompare(str2)); // -1
-
codePointAt() - Returns the Unicode code point of the character at a specified index.
let str = "hello";
console.log(str.codePointAt(0)); // 104
-
normalize() - Returns the Unicode normalization form of the string.
let str = "e\u0301";
console.log(str.normalize("NFC")); // "é"
-
toString() - Returns the string representation of the value.
let str = new String("hello");
console.log(str.toString()); // "hello"