In JavaScript, strings come with a rich set of methods that allow you to manipulate and interact with them in various ways. Here are some of the most common methods:
charAt()
Description: Returns the character at the specified index (position).
Syntax: string.charAt(index)
Example:
let str = "Hello";
console.log(str.charAt(0)); // Output: H
charCodeAt()
Description: Returns the Unicode of the character at the specified index.
Syntax: string.charCodeAt(index)
Example:
let str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72
concat()
Description: Joins two or more strings and returns a new joined string.
Syntax: string.concat(string1, string2, ...)
Example:
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: Hello World
includes()
Description: Checks whether a string contains the specified string/characters.
Syntax: string.includes(searchString)
Example:
let str = "Hello World";
console.log(str.includes("World")); // Output: true
endsWith()
Description: Checks whether a string ends with specified string/characters.
Syntax: string.endsWith(searchString)
Example:
let str = "Hello World";
console.log(str.endsWith("World")); // Output: true
indexOf()
Description: Returns the index of the first occurrence of a specified value in a string.
Syntax: string.indexOf(searchValue)
Example:
let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6
lastIndexOf()
Description: Returns the index of the last occurrence of a specified value in a string.
Syntax: string.lastIndexOf(searchValue)
Example:
let str = "Hello World World";
console.log(str.lastIndexOf("World")); // Output: 12
match()
Description: Searches a string for a match against a regular expression, and returns the matches.
Syntax: string.match(regexp)
Example:
let str = "Hello World";
console.log(str.match(/World/g)); // Output: ["World"]
matchAll()
Description: Returns an iterator of all matches of a string against a regular expression.
Syntax: string.matchAll(regexp)
Example:
let str = "Hello World World";
const matches = str.matchAll(/World/g);
for (const match of matches) {
console.log(match[0]); // Output: World
}
normalize()
Description: Returns the Unicode Normalization Form of the string.
Syntax: string.normalize(form)
Example:
let str = "é";
console.log(str.normalize("NFD")); // Output: é
padEnd()
Description: Pads the current string with another string until the resulting string reaches the given length.
Syntax: string.padEnd(length, padString)
Example:
let str = "Hello";
console.log(str.padEnd(10, ".")); // Output: Hello.....
padStart()
Description: Pads the current string with another string until the resulting string reaches the given length.
Syntax: string.padStart(length, padString)
Example:
let str = "Hello";
console.log(str.padStart(10, ".")); // Output: .....Hello
repeat()
Description: Returns a new string with a specified number of copies of the string.
Syntax: string.repeat(count)
Example:
let str = "Hello";
console.log(str.repeat(3)); // Output: HelloHelloHello
replace()
Description: Searches a string for a specified value, and returns a new string where the specified values are replaced.
Syntax: string.replace(searchValue, newValue)
Example:
let str = "Hello World";
console.log(str.replace("World", "Everyone")); // Output: Hello Everyone
replaceAll()
Description: Returns a new string with all matches of a pattern replaced by a replacement.
Syntax: string.replaceAll(searchValue, newValue)
Example:
let str = "Hello World World";
console.log(str.replaceAll("World", "Everyone")); // Output: Hello Everyone Everyone
search()
Description: Searches a string for a specified value and returns the position of the match.
Syntax: string.search(searchValue)
Example:
let str = "Hello World";
console.log(str.search("World")); // Output: 6
slice()
Description: Extracts a part of a string and returns a new string.
Syntax: string.slice(startIndex, endIndex)
Example:
let str = "Hello World";
console.log(str.slice(0, 5)); // Output: Hello
split()
Description: Splits a string into an array of substrings.
Syntax: string.split(separator)
Example:
let str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]
startsWith()
Description: Checks whether a string begins with the characters of a specified string.
Syntax: string.startsWith(searchString)
Example:
let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true
substring()
Description: Returns the part of the string between two specified indices.
Syntax: string.substring(startIndex, endIndex)
Example:
let str = "Hello World";
console.log(str.substring(0, 5)); // Output: Hello
toLowerCase()
Description: Returns the calling string value converted to lowercase.
Syntax: string.toLowerCase()
Example:
let str = "Hello World";
console.log(str.toLowerCase()); // Output: hello world
toUpperCase()
Description: Returns the calling string value converted to uppercase.
Syntax: string.toUpperCase()
Example:
let str = "Hello World";
console.log(str.toUpperCase()); // Output: HELLO WORLD
trim()
Description: Removes whitespace from both ends of a string.
Syntax: string.trim()
Example:
let str = " Hello World ";
console.log(str.trim()); // Output: Hello World
trimEnd()
Description: Removes whitespace from the end of a string.
Syntax: string.trimEnd()
Example:
let str = " Hello World ";
console.log(str.trimEnd()); // Output: " Hello World"
trimStart()
Description: Removes whitespace from the beginning of a string.
Syntax: string.trimStart()
Example:
let str = " Hello World ";
console.log(str.trimStart()); // Output: "Hello World "
valueOf()
Description: Returns the primitive value of a String object.
Syntax: string.valueOf()
Example:
let str = new String("Hello");
console.log(str.valueOf()); // Output: Hello