Aral Code

Variables & Data Lesson 9 of 30

JavaScript String Methods

Useful string methods — toUpperCase, toLowerCase, trim, includes, startsWith, indexOf, slice, replace, split and padStart — with school and store examples.

A method is a built-in action you can ask a value to do. You write the value, a dot, the method name and parentheses: name.toUpperCase(). Strings come with dozens of methods. Here are the ones you'll use most.

Changing case

Example toUpperCase and toLowerCase
<pre id="out"></pre>
<script>
  const name = "maria clara";
  document.getElementById("out").textContent =
    name.toUpperCase() + "\n" +
    "MABUHAY".toLowerCase();
</script>

Remember: strings can't be changed, so these methods return a new string. name itself is still "maria clara". To keep the result, store it: const upper = name.toUpperCase();.

trim(): remove extra spaces

Users often type spaces by accident before or after their input. trim() removes them from both ends:

"   juan@email.com  ".trim()   // "juan@email.com"

Searching inside a string

MethodReturnsExample → result
includes(x)true/false"Grade 10-Rizal".includes("Rizal") → true
startsWith(x)true/false"09171234567".startsWith("09") → true
endsWith(x)true/false"photo.jpg".endsWith(".jpg") → true
indexOf(x)position, or -1"banana".indexOf("n") → 2

All of these are case-sensitive: "Rizal".includes("rizal") is false. Convert both sides to lowercase first if case shouldn't matter.

slice(): cutting out a part

slice(start, end) returns the characters from start up to, but not including, end:

Example Getting parts of a student ID
<pre id="out"></pre>
<script>
  const id = "2026-10-0453";
  const year = id.slice(0, 4);
  const grade = id.slice(5, 7);
  const number = id.slice(-4);   // negative counts from the end
  document.getElementById("out").textContent =
    `Year: ${year}\nGrade: ${grade}\nNumber: ${number}`;
</script>

replace() and replaceAll()

"I love Math".replace("Math", "ICT")        // "I love ICT"
"a-b-c".replace("-", " ")                   // "a b-c"  (only the first)
"a-b-c".replaceAll("-", " ")                // "a b c"

split(): string to list

split() breaks a string into an array (a list) wherever it finds a separator:

Example Splitting a list of subjects
<pre id="out"></pre>
<script>
  const subjects = "Math,Science,English,Filipino";
  const list = subjects.split(",");
  document.getElementById("out").textContent =
    "Number of subjects: " + list.length + "\n" +
    "Second subject: " + list[1];
</script>

.split(" ") splits a sentence into words, and .split("") splits it into single characters.

padStart(): fixed-width numbers

padStart(length, filler) adds characters at the beginning until the string is long enough. Perfect for receipt numbers and clocks:

String(7).padStart(3, "0")     // "007"
String(45).padStart(5, "0")    // "00045"

Chaining methods

Because each method returns a new string, you can call another method right after it:

Example Cleaning up a typed name
<p id="out"></p>
<script>
  const typed = "   JUAN dela cruz   ";
  const clean = typed.trim().toLowerCase();
  document.getElementById("out").textContent = `"${clean}"`;
</script>

A practical example: is it a valid PH mobile number?

Example Simple mobile number check
<input id="cp" placeholder="09XXXXXXXXX">
<button onclick="check()">Check</button>
<p id="msg"></p>
<script>
  function check() {
    const num = document.getElementById("cp").value.trim().replaceAll(" ", "");
    let msg;
    if (num.length !== 11) msg = "❌ Must be 11 digits.";
    else if (!num.startsWith("09")) msg = "❌ Must start with 09.";
    else msg = "✅ Looks good!";
    document.getElementById("msg").textContent = msg;
  }
</script>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does " hello ".trim() return?
2. What does "Pilipinas".slice(0, 4) return?
3. What does "Math,Science".split(",") return?
4. After let s = "abc"; s.toUpperCase();, what is s?

By Sir Reyel · Last updated 2026-12-04