Common String Methods0%
Common String Methods
Beginner12 min read•Updated: Jul 11, 2026
Common String Methods in JavaScript
JavaScript provides a rich library of built-in methods on strings to search, extract, replace, format, and split text. Because strings are immutable, all string methods return a new string without modifying the original.
1. Case Transformation & Trimming
JavaScript
const rawInput = " sumit sharma ";
// 1. Convert to Uppercase
console.log(rawInput.toUpperCase()); // " SUMIT SHARMA "
// 2. Convert to Lowercase
console.log(rawInput.toLowerCase()); // " sumit sharma "
// 3. Trim whitespace from both ends (Essential for form inputs!)
const cleanInput = rawInput.trim();
console.log(cleanInput); // "sumit sharma"
// Trim only start or only end:
console.log(rawInput.trimStart());
console.log(rawInput.trimEnd());
2. Searching in Strings
A. .includes() (ES6)
Checks if a substring exists; returns true or false (case-sensitive):
JavaScript
const email = "student@mskinstitute.com";
console.log(email.includes("@")); // true
console.log(email.includes("yahoo")); // false
B. .startsWith() and .endsWith() (ES6)
JavaScript
const filename = "report_q3_final.pdf";
console.log(filename.startsWith("report")); // true
console.log(filename.endsWith(".pdf")); // true
C. .indexOf() and .lastIndexOf()
Returns the starting index of substring, or -1 if not found:
JavaScript
const phrase = "Learn JavaScript at MSK";
console.log(phrase.indexOf("JavaScript")); // 6
console.log(phrase.indexOf("Python")); // -1
3. Extracting Substrings: .slice()
The .slice(startIndex, endIndex) method extracts a portion of a string:
startIndex: Index where extraction begins (inclusive).endIndex(optional): Index where extraction ends (exclusive).- Supports negative indices to count backward from the end!
JavaScript
const text = "JavaScript";
console.log(text.slice(0, 4)); // "Java" (indices 0, 1, 2, 3)
console.log(text.slice(4)); // "Script" (from index 4 to the end)
console.log(text.slice(-6)); // "Script" (last 6 characters)
4. Replacing Text: .replace() and .replaceAll()
JavaScript
const message = "JavaScript is slow. JavaScript is hard.";
// .replace() only replaces the FIRST occurrence:
console.log(message.replace("JavaScript", "Python"));
// Output: "Python is slow. JavaScript is hard."
// .replaceAll() (ES2021) replaces ALL occurrences:
console.log(message.replaceAll("JavaScript", "JS"));
// Output: "JS is slow. JS is hard."
5. Splitting Strings into Arrays: .split()
.split(delimiter) splits a string into an array of substrings based on a separator:
JavaScript
// Split by comma:
const tags = "react,javascript,frontend,css";
const tagList = tags.split(",");
console.log(tagList); // ["react", "javascript", "frontend", "css"]
// Split by space (words):
const sentence = "Welcome to MSK Institute";
const words = sentence.split(" ");
console.log(words); // ["Welcome", "to", "MSK", "Institute"]
// Split into individual characters:
const letters = "MSK".split("");
console.log(letters); // ["M", "S", "K"]
Interactive Quiz
Practice Quiz
Question 1
What does
str.trim() do?Question 2
What is returned by
"Frontend".slice(0, 5)?Question 3
If
str = "apple,banana,orange", what does str.split(",") return?Question 4
What does
"coding".includes("od") evaluate to?Question 5
What is the output of
"hello".indexOf("z")?Next Lesson
Template Literals (Basics)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| String Basics | Template Literals (Basics) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.