New String Methods
Explore ES6 string methods — startsWith, endsWith, includes, repeat, padStart, padEnd, and more.
New String Methods
ES6 added several useful methods to the String prototype that replace clunky workarounds with clean, readable code.
startsWith()
const url = "https://example.com/api/users";
url.startsWith("https"); // true
url.startsWith("http://"); // false
url.startsWith("example", 8); // true (start searching at index 8)
Replaces: url.indexOf("https") === 0
endsWith()
const file = "report.pdf";
file.endsWith(".pdf"); // true
file.endsWith(".doc"); // false
file.endsWith("port", 6); // true (check only first 6 characters: "report")
Replaces: file.indexOf(".pdf") === file.length - 4
includes()
const sentence = "The quick brown fox jumps over the lazy dog";
sentence.includes("fox"); // true
sentence.includes("cat"); // false
sentence.includes("quick", 5); // false (starts searching at index 5)
Replaces: sentence.indexOf("fox") !== -1
repeat()
"ha".repeat(3); // "hahaha"
"abc".repeat(0); // ""
"=-".repeat(20); // "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
// Practical: padding
function padLeft(str, length, char = " ") {
const padding = char.repeat(Math.max(0, length - str.length));
return padding + str;
}
padLeft("42", 5, "0"); // "00042"
padStart() and padEnd() (ES2017)
// padStart — pad from the beginning
"42".padStart(5, "0"); // "00042"
"hi".padStart(10); // " hi"
// padEnd — pad from the end
"42".padEnd(5, "0"); // "42000"
"hi".padEnd(10, "."); // "hi........"
// Practical: time formatting
const hours = "9";
const minutes = "5";
const time = `${hours.padStart(2, "0")}:${minutes.padStart(2, "0")}`;
// "09:05"
trimStart() and trimEnd() (ES2019)
const messy = " hello ";
messy.trim(); // "hello" (both sides)
messy.trimStart(); // "hello " (left only)
messy.trimEnd(); // " hello" (right only)
All New String Methods
| Method | What It Does | Returns |
|---|---|---|
startsWith(str) | Checks if string starts with str | Boolean |
endsWith(str) | Checks if string ends with str | Boolean |
includes(str) | Checks if string contains str | Boolean |
repeat(n) | Repeats string n times | String |
padStart(len, char) | Pads start to reach len | String |
padEnd(len, char) | Pads end to reach len | String |
trimStart() | Removes leading whitespace | String |
trimEnd() | Removes trailing whitespace | String |
Practical Example: URL Validator
function isSecureUrl(url) {
return url.startsWith("https://");
}
function isImageFile(filename) {
return filename.endsWith(".jpg") ||
filename.endsWith(".png") ||
filename.endsWith(".gif");
}
function containsKeyword(text, keyword) {
return text.toLowerCase().includes(keyword.toLowerCase());
}
isSecureUrl("https://example.com"); // true
isImageFile("photo.png"); // true
containsKeyword("Hello World", "world"); // true
Key Takeaways
startsWith(),endsWith(),includes()replaceindexOfchecksrepeat()creates repeated strings without loopspadStart()/padEnd()are perfect for formatting numbers and alignment- All methods are case-sensitive — use
toLowerCase()for case-insensitive checks - These methods return new strings — strings are immutable in JavaScript