Skip to content

String tools

Read · 1 of 4

Handy methods

s.slice(a, b) is the part from position a up to (not including) b. s.indexOf("x") finds where something first appears (−1 if it doesn't). s.toUpperCase() and s.includes(t) do what they say.

Strings can't be changed in place. To reverse one: s.split("").reverse().join("") (split into characters, reverse the array, join back).

s.charCodeAt(i) - 97 is 0 for 'a', 1 for 'b', and so on. That turns letters into array positions.