Episode 7 of 11

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

MethodWhat It DoesReturns
startsWith(str)Checks if string starts with strBoolean
endsWith(str)Checks if string ends with strBoolean
includes(str)Checks if string contains strBoolean
repeat(n)Repeats string n timesString
padStart(len, char)Pads start to reach lenString
padEnd(len, char)Pads end to reach lenString
trimStart()Removes leading whitespaceString
trimEnd()Removes trailing whitespaceString

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() replace indexOf checks
  • repeat() creates repeated strings without loops
  • padStart() / 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