</>
Skip to content
JavaScript lessons (19/64)

JavaScript — String Methods

Remember the rule

Strings never change — every method returns a new string:

const msg = "  Hello World  ";
msg.toUpperCase();   // "  HELLO WORLD  " (new)
msg;                 // "  Hello World  " (untouched)

Case

"hi".toUpperCase();     // "HI"
"HI".toLowerCase();     // "hi"   ← normalize before comparing!

Case-insensitive check idiom:

input.toLowerCase() === "yes"

Extracting pieces

const email = "ada@lovelace.dev";

email.slice(0, 3);       // "ada"      start index, end (excluded)
email.slice(4);          // "lovelace.dev"  to the end
email.slice(-4);         // ".dev"     negatives count from the right
email.at(-1);            // "v"        modern last-char access

Finding things

"banana".indexOf("na");      // 2   first position, -1 if absent
"banana".includes("ana");    // true
"banana".startsWith("ban");  // true
"file.PDF".endsWith(".pdf"); // false — case matters!

includes/startsWith/endsWith return booleans — perfect for if; indexOf when you need where.

Replacing

"a-b-c".replace("-", "+");        // "a+b-c"  FIRST occurrence only!
"a-b-c".replaceAll("-", "+");     // "a+b+c"
"Hello world".replace(/o/g, "0"); // regex flag g = every match

The first-occurrence-only surprise is a rite of passage.

Trimming whitespace

"  hi \n".trim();       // "hi"
"  hi".trimStart();     // left only
"hi  ".trimEnd();       // right only

Mandatory cleanup for form input and pasted data.

Splitting & joining

"red,green,blue".split(",");   // ["red","green","blue"] → ARRAY
["a","b"].join("-");           // "a-b"                  → STRING
"abc".split("");               // ["a","b","c"] chars

Split/join combo does global replaces without regex:

path.replaceAll("\\", "/");    // or: path.split("\\").join("/")

Padding & repeating

"5".padStart(3, "0");   // "005" — clock/ticket formatting
"7".padEnd(3, ".");     // "7.."
"ab ".repeat(3);        // "ab ab ab "

Chaining — the real style

Methods compose because each returns a string:

const raw = "  Ada Lovelace  ";
const handle = raw.trim().toLowerCase().replaceAll(" ", ".");
// "ada.lovelace"

Read left-to-right like a pipeline: trim → lower → join with dots.

Mini Practice

  1. Normalize five user emails: trim + lowercase + verify includes("@")
  2. Extract domain from "user@example.com" using slice/indexOf
  3. Global-replace every comma in a sentence with a line break (\n)
  4. Format seconds 5→"05", 105→"1:45" using padStart + arithmetic
  5. Build filename checker: endsWith(".pdf"), case-insensitive

Next: numbers →

Related Topics

Frequently Asked Questions about String Methods

What is String Methods in JavaScript?

String Methods is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn String Methods?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn String Methods.

Why is String Methods important in JavaScript?

String Methods is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.