Loading lessons...
String and Array Methods Reference
String and Array Methods Reference
The most useful string and array methods at a glance.
String methods
| Method | Purpose |
|---|---|
length | number of characters |
toUpperCase() | make uppercase |
toLowerCase() | make lowercase |
trim() | remove spaces at ends |
slice(a, b) | extract a section |
split(sep) | turn into an array |
includes(text) | does it contain text |
replace(a, b) | swap text |
indexOf(text) | first position or -1 |
Array methods
| Method | Purpose |
|---|---|
length | number of items |
push(x) | add at end |
pop() | remove last |
shift() | remove first |
unshift(x) | add at start |
includes(x) | does it contain x |
indexOf(x) | position of x or -1 |
sort() | sort items |
join(sep) | turn into a string |
map(fn) | transform each item |
filter(fn) | keep matches |
reduce(fn, init) | combine into one |
split and join
split string to array; join array to string:
"a,b".split(","); // ["a", "b"]
["a", "b"].join(","); // "a,b"
TL;DR
- Strings: toUpperCase, split, slice, includes.
- Arrays: push, pop, map, filter, reduce.
- split and join are opposites.