Clean
trim removes whitespace at the start and end.
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 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.
trim removes whitespace at the start and end.
includes, startsWith and endsWith answer yes/no questions.
slice returns part of a string by index range.
replace, split, toLowerCase and toUpperCase create new values.
Examples
const rawSearch = " JAVASCRIPT "; const search = rawSearch.trim().toLowerCase(); console.log(search); // "javascript"
let search = " JAVASCRIPT "; search.trim(); search.toLowerCase(); console.log(search); // still has spaces and uppercase letters
Code patterns
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.
Trim first when surrounding spaces do not matter.
const raw = " dashboard "; const clean = raw.trim(); console.log(clean); // "dashboard"
Normalize both sides before checking.
const title = "JavaScript String Methods"; const query = "script"; const found = title.toLowerCase().includes(query.toLowerCase()); console.log(found);
slice is useful for stable positions.
const code = "JS-2026-001"; const prefix = code.slice(0, 2); console.log(prefix); // "JS"
split turns text into an array.
const tags = "html,css,javascript";
const list = tags.split(",");
console.log(list);
Rules that matter
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.
String methods usually return a new string.
Trim and case-normalize when exact raw text is not required.
slice is predictable and works with negative indexes.
It replaces all matching plain strings. Regular expressions have their own rules.
Make sure the separator exists and matches the data format.
Name indexes or document formats when extracting fixed positions.
Production thinking
Text arrives messy. String methods turn raw text into predictable data without writing custom parsing for every small task.
Clean strings produce cleaner labels, messages and form feedback. Poor string handling can create confusing visible text.
Production string processing should be explicit about formats, casing, separators and fallback behavior.
Slug generation, titles, descriptions and headings often depend on string methods. Mistakes can create ugly or duplicated URLs and metadata.
Live code lab
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
Practice assignment
Try it yourself
Self-check
Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.