10 JavaScript String Methods You Should Know
Discover the 10 essential string methods to stand out as a JavaScript developer. Level up your skills now!
• 3 min
Strings play a crucial role in JavaScript programming, used for storing and manipulating text. In this article, you’ll see 10 essential JavaScript string methods you need to know to work efficiently.
1. startsWith()
The startsWith() method checks whether a string starts with the specified value. It returns true if the string starts with the value and false otherwise.
Syntax:
string.startsWith(searchString[, position])
Example:
const text = "Hello, world!";
const startsWithHello = text.startsWith("Hello");
console.log(startsWithHello); // true
2. endsWith()
The endsWith() method checks whether a string ends with the specified value. It returns true if the string ends with the value and false otherwise.
Syntax:
string.endsWith(searchString[, length])
Example:
const text = "Hello, world!";
const endsWithWorld = text.endsWith("world!");
console.log(endsWithWorld); // true
3. includes()
The includes() method checks whether a string contains a specified value. It returns true if the string contains the value and false otherwise.
Syntax:
string.includes(searchString[, position])
Example:
const text = "Hello, world!";
const containsHello = text.includes("Hello");
console.log(containsHello); // true
4. slice()
The slice() method is a string manipulation function in JavaScript that lets you extract a specific part of a string and return it as a new string.
Syntax:
string.slice(beginIndex[, endIndex])
Example:
const text = "Hello, world!";
const part = text.slice(7, 12);
console.log(part); // "world"
5. toUpperCase()
The toUpperCase() method converts all characters in a string to uppercase and returns the new string.
Syntax:
string.toUpperCase()
Example:
const text = "Hello, world!";
const uppercase = text.toUpperCase();
console.log(uppercase); // "HELLO, WORLD!"
6. toLowerCase()
The toLowerCase() method converts all characters in a string to lowercase and returns the new string.
Syntax:
string.toLowerCase()
Example:
const text = "Hello, world!";
const lowercase = text.toLowerCase();
console.log(lowercase); // "hello, world!"
7. charAt()
The charAt() method returns the character at the specified position in a string.
Syntax:
string.charAt(index)
Example:
const text = "Hello, world!";
const character = text.charAt(7);
console.log(character); // "w"
8. split()
The split() method splits a string into an array of substrings based on a specified separator.
Syntax:
string.split(separator[, limit])
Example:
const text = "Hello, world!";
const words = text.split(" ");
console.log(words); // ["Hello,", "world!"]
9. replace()
The replace() method replaces the first occurrence of a value with another in a string.
Syntax:
string.replace(searchValue, replaceValue)
Example:
const text = "Hello, world!";
const newText = text.replace("world", "friend");
console.log(newText); // "Hello, friend!"
10. repeat()
The repeat() method creates and returns a new string with a specified number of copies of the original string.
Syntax:
string.repeat(count)
Example:
const word = "Go ";
const repetitions = word.repeat(3);
console.log(repetitions); // "Go Go Go "
Mastering these JavaScript string methods is fundamental to handling text effectively and performing a variety of string manipulation operations in your development projects. Each of these methods offers specific functionality that can make your code more efficient and readable.
Citations
- 10 JavaScript string methods you should knowFrugence Fidel
- JavaScript String MethodsW3 Schools