Cheat Sheets

JavaScript String Methods Cheat Sheet: Slice, Split, Replace & Template Literals

By ArpaNeuro Team September 12, 2026
JavaScript strings cheat sheet

JavaScript strings are immutable UTF-16 sequences. Methods like slice, split, trim, and replaceAll return new strings. Template literals interpolate values and multi-line text. Do not use length as a character count when emoji or combining marks matter — use Intl.Segmenter or [...str].

This JavaScript cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.

Quick reference

  • slice: str.slice(0, 8) copy. Negative indexes count from the end.
  • split / join: csv.split(",") and parts.join(" · ").
  • trim: trim, trimStart, trimEnd for form input.
  • search: includes, startsWith, endsWith — case-sensitive. Normalize with toLowerCase.
  • replace: replace changes one match unless you pass /g. replaceAll needs a string or global regex.
  • pad: padStart(2, "0") for clocks and invoice numbers.
  • template: ` Hello ${name} `. Escape user HTML before inserting into the DOM.
  • Number: Number.parseInt(str, 10) and Number(str) — watch empty string → 0.

Copy-paste examples

Slug and display helpers

Normalize once, then compare. replaceAll is clearer than /g for literal separators.

function slugify(title) {
  return title
    .trim()
    .toLowerCase()
    .replaceAll(' ', '-')
    .replaceAll(/[^a-z0-9-]/g, '');
}
function initials(name) {
  return name
    .trim()
    .split(/\s+/)
    .map((p) => p[0])
    .join('')
    .toUpperCase();
}

Common mistakes

  • Mutating thinking: str.replace does nothing unless you assign the return value.
  • parseInt("08") without radix in old engines; always pass 10.
  • Building HTML with template literals from user input (XSS).
  • Using split("") on emoji strings and breaking characters.

FAQ

  1. String vs String object? Prefer primitives "hi". new String() boxes and fails some === checks. You almost never need the constructor.
  1. BLOGPH0 for strings? Use ===. Coercion with numbers ("10" == 10) hides bugs.
  1. How do I pad a month? String(month).padStart(2, "0")"09".

Related JavaScript cheat sheets

Build with this stack

When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds custom web development and also sells ready-made source code. Request a quote and tell us the stack.

Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.

← All Articles Get a Quote →