FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Strings, Numbers & Math

Intermediate

String Methods

String methods let you search, clean, slice, replace and transform text. They return new values, because strings are immutable.

const input = "  JavaScript Basics  ";

input.trim().toLowerCase();
input.includes("Script");
input.slice(2, 12);

Strings, Numbers & Math

String methods are small tools for text processing.

String methods are functions available on string values. Common methods include trim, toLowerCase, toUpperCase, includes, startsWith, endsWith, slice, replace, replaceAll, split and padStart.

Most string methods do not change the original string. They return a new string or a result such as true, false, an index or an array.

The best way to learn string methods is by pattern: clean input, search text, extract part of a value, replace text, split text into pieces and format output.

Clean

trim removes whitespace at the start and end.

Search

includes, startsWith and endsWith answer yes/no questions.

Extract

slice returns part of a string by index range.

Transform

replace, split, toLowerCase and toUpperCase create new values.

Examples

Start with the smallest working pattern.

Chain small methods when the order is clear

const rawSearch = "  JAVASCRIPT  ";
const search = rawSearch.trim().toLowerCase();

console.log(search); // "javascript"

Forgetting that methods return a new value

let search = "  JAVASCRIPT  ";

search.trim();
search.toLowerCase();

console.log(search); // still has spaces and uppercase letters

Code patterns

Reusable examples for quick reference.

These small examples are designed for scanning. Use them when you need the syntax quickly, then read the surrounding notes when you want the deeper reason behind the pattern.

Clean input

Trim first when surrounding spaces do not matter.

const raw = "  dashboard  ";
const clean = raw.trim();

console.log(clean); // "dashboard"

Case-insensitive search

Normalize both sides before checking.

const title = "JavaScript String Methods";
const query = "script";

const found = title.toLowerCase().includes(query.toLowerCase());
console.log(found);

Extract a prefix

slice is useful for stable positions.

const code = "JS-2026-001";
const prefix = code.slice(0, 2);

console.log(prefix); // "JS"

Split text into pieces

split turns text into an array.

const tags = "html,css,javascript";
const list = tags.split(",");

console.log(list);

Rules that matter

Keep text and numbers predictable.

Strings and numbers are simple until they cross a boundary: form input, API data, generated output, rounding, formatting or search. Normalize, validate and format deliberately.

Store returned values

String methods usually return a new string.

Normalize before search

Trim and case-normalize when exact raw text is not required.

Choose slice over substring in new code

slice is predictable and works with negative indexes.

Use replaceAll deliberately

It replaces all matching plain strings. Regular expressions have their own rules.

Split with a known separator

Make sure the separator exists and matches the data format.

Avoid magic indexes

Name indexes or document formats when extracting fixed positions.

Production thinking

Small value mistakes become visible product mistakes.

Why does this matter?

Text arrives messy. String methods turn raw text into predictable data without writing custom parsing for every small task.

Accessibility

Clean strings produce cleaner labels, messages and form feedback. Poor string handling can create confusing visible text.

Production note

Production string processing should be explicit about formats, casing, separators and fallback behavior.

SEO note

Slug generation, titles, descriptions and headings often depend on string methods. Mistakes can create ugly or duplicated URLs and metadata.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Add replaceAll("&", "and") to the slug function.
  • Use includes to show whether the title contains JavaScript.
  • Use slice to show the first five characters.

Practice assignment

Do this before moving to the next topic.

  1. Clean a string with trim.
  2. Search a string with includes.
  3. Split a comma-separated string into an array.

Try it yourself

Create a simple slug

Live preview

Self-check

Before you continue, prove that you understand String Methods.

Intermediate

Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.

  1. Can you explain why trim does not change the original string?
  2. Can you explain when includes is useful?
  3. Can you explain what slice returns?
  4. Can you explain why split returns an array?
  5. Can you explain why fixed indexes need care?