JavaScript string methods are built-in functions that allow you to manipulate and work with strings in various ways.
These methods make it easier to perform operations like finding, replacing, splitting, or modifying strings.
Common JavaScript String Methods
length
- Returns the number of characters in a string.
let text = "JavaScript"; console.log(text.length); // Outputs: 10
toUpperCase()
- Converts all characters in a string to uppercase.
let text = "JavaScript"; console.log(text.toUpperCase()); // Outputs: "JAVASCRIPT"
toLowerCase()
- Converts all characters in a string to lowercase.
let text = "JavaScript"; console.log(text.toLowerCase()); // Outputs: "javascript"
charAt(index)
- Returns the character at a specific index in a string.
let text = "JavaScript"; console.log(text.charAt(0)); // Outputs: "J"
indexOf(substring)
- Returns the index of the first occurrence of a specified substring, or
-1
if the substring is not found.
let text = "JavaScript"; console.log(text.indexOf("Script")); // Outputs: 4
- Returns the index of the first occurrence of a specified substring, or
lastIndexOf(substring)
- Returns the index of the last occurrence of a specified substring, or
-1
if not found.
let text = "JavaScript is great, and I love JavaScript!"; console.log(text.lastIndexOf("JavaScript")); // Outputs: 34
- Returns the index of the last occurrence of a specified substring, or
includes(substring)
- Returns
true
if a string contains a specified substring, otherwisefalse
.
let text = "JavaScript is fun"; console.log(text.includes("fun")); // Outputs: true
- Returns
startsWith(substring)
- Returns
true
if a string starts with a specified substring, otherwisefalse
.
let text = "JavaScript"; console.log(text.startsWith("Java")); // Outputs: true
- Returns
endsWith(substring)
- Returns
true
if a string ends with a specified substring, otherwisefalse
.
let text = "JavaScript"; console.log(text.endsWith("Script")); // Outputs: true
- Returns
substring(start, end)
- Extracts characters from a string between two specified indices.
let text = "JavaScript"; let part = text.substring(0, 4); // "Java"
slice(start, end)
- Similar to
substring
, but can also accept negative indices to count from the end.
let text = "JavaScript"; let part = text.slice(4); // "Script"
- Similar to
replace(searchValue, newValue)
- Replaces the first occurrence of a specified substring with a new value.
let text = "JavaScript is great"; let newText = text.replace("great", "awesome"); console.log(newText); // Outputs: "JavaScript is awesome"
replaceAll(searchValue, newValue)
- Replaces all occurrences of a specified substring with a new value.
let text = "JavaScript is great. JavaScript is popular."; let newText = text.replaceAll("JavaScript", "JS"); console.log(newText); // Outputs: "JS is great. JS is popular."
split(separator)
- Splits a string into an array of substrings based on a specified separator.
let text = "JavaScript,HTML,CSS"; let parts = text.split(","); console.log(parts); // Outputs: ["JavaScript", "HTML", "CSS"]
trim()
- Removes whitespace from both ends of a string.
let text = " JavaScript "; console.log(text.trim()); // Outputs: "JavaScript"
trimStart()
(ortrimLeft()
)- Removes whitespace from the beginning of a string.
let text = " JavaScript"; console.log(text.trimStart()); // Outputs: "JavaScript"
trimEnd()
(ortrimRight()
)- Removes whitespace from the end of a string.
let text = "JavaScript "; console.log(text.trimEnd()); // Outputs: "JavaScript"
concat(string1, string2, ...)
- Joins two or more strings and returns a new concatenated string.
let text1 = "Hello"; let text2 = "World"; let result = text1.concat(", ", text2); console.log(result); // Outputs: "Hello, World"
repeat(count)
- Returns a new string with a specified number of copies of the original string.
let text = "JavaScript"; console.log(text.repeat(3)); // Outputs: "JavaScriptJavaScriptJavaScript"
padStart(targetLength, padString)
- Pads the current string with another string until it reaches the target length, starting from the beginning.
let text = "5"; console.log(text.padStart(3, "0")); // Outputs: "005"
padEnd(targetLength, padString)
- Pads the current string with another string until it reaches the target length, starting from the end.
let text = "5"; console.log(text.padEnd(3, "0")); // Outputs: "500"
match(regex)
- Searches a string for a match against a regular expression and returns the matches.
let text = "The rain in Spain"; let result = text.match(/ain/g); console.log(result); // Outputs: ["ain", "ain"]
search(regex)
- Searches a string for a match against a regular expression and returns the index of the match, or
-1
if not found.
let text = "JavaScript"; console.log(text.search(/Script/)); // Outputs: 4
- Searches a string for a match against a regular expression and returns the index of the match, or
toString()
- Returns the string representation of a value.
let number = 123; console.log(number.toString()); // Outputs: "123"
valueOf()
- Returns the primitive value of a string object.
let text = new String("Hello"); console.log(text.valueOf()); // Outputs: "Hello"
These examples cover a wide range of string manipulation techniques available in JavaScript, providing powerful tools for working with text in your programs.