typestar

String methods in JavaScript

The methods worth knowing, including the newer ones.

const title = "  Language Basics  ";

console.log(title.trim().toLowerCase().replaceAll(" ", "-"));
console.log(title.trimStart().length, title.trimEnd().length);
console.log("go".padStart(6, "."), "go".padEnd(6, "."));
console.log("typestar".at(0), "typestar".at(-1));
console.log("a,b,,c".split(",").filter(Boolean));
console.log("abc".includes("b"), "abc".startsWith("a"), "abc".indexOf("z"));

const accented = "h\u00e9llo";
console.log(accented.length, [...accented].length,
            accented.normalize("NFD").length);
console.log("x".repeat(3), "Typestar".localeCompare("typestar"));

How it works

  1. at(-1) reads from the end, which bracket indexing cannot.
  2. padStart and trimEnd do the alignment work.
  3. A string is UTF-16 code units, so spreading it is safer than length.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
580
Tokens
183
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 129 seconds.

Type this snippet

Step 1 of 5 in Text, step 6 of 43 in Language basics.

← Previous Next →