Common String Methods
| Method | Example | Result |
.length | "hello".length | 5 |
.trim() | " hi ".trim() | "hi" |
.toUpperCase() | "hi".toUpperCase() | "HI" |
.toLowerCase() | "HI".toLowerCase() | "hi" |
.includes() | "hello".includes("ell") | true |
.startsWith() | "hello".startsWith("he") | true |
.endsWith() | "hello".endsWith("lo") | true |
.indexOf() | "hello".indexOf("l") | 2 |
.slice() | "hello".slice(1, 3) | "el" |
.replace() | "hi hi".replace("hi","yo") | "yo hi" |
.replaceAll() | "hi hi".replaceAll("hi","yo") | "yo yo" |
.split() | "a,b,c".split(",") | ["a","b","c"] |
.repeat() | "ha".repeat(3) | "hahaha" |
.padStart() | "5".padStart(3,"0") | "005" |
.at() | "hello".at(-1) | "o" |
Template Literals
const name = "World";
`Hello, ${name}!` // "Hello, World!"
`Multi
line` // Works!
`Result: ${2 + 3}` // "Result: 5"
Regex with Strings
// Test a pattern
/^\d+$/.test("123") // true
// Match
"abc123".match(/\d+/) // ["123"]
// Replace with regex
"hello world".replace(/\w+/g, w =>
w[0].toUpperCase() + w.slice(1)
) // "Hello World"