JavaScript Methods
String and Array methods with syntax, examples, and return values
String — Properties & Search
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
.length
|
str.length |
'hello'.length |
5 | Number of UTF-16 code units in string (number) |
.charAt(i)
|
str.charAt(i) |
'abc'.charAt(1) |
'b' | Character at index i; returns '' if out of bounds |
.indexOf(s)
|
str.indexOf(s, from) |
'hello'.indexOf('l') |
2 | First index of s; -1 if not found; optional start position |
.lastIndexOf(s)
|
str.lastIndexOf(s) |
'abab'.lastIndexOf('a') |
2 | Last index of s; -1 if not found |
.includes(s)
|
str.includes(s, from) |
'hello'.includes('ell') |
true | Returns true if s is found anywhere in string |
.startsWith(s)
|
str.startsWith(s, pos) |
'hello'.startsWith('he') |
true | True if string starts with s; optional start position |
.endsWith(s)
|
str.endsWith(s, len) |
'hello'.endsWith('lo') |
true | True if string ends with s; optional length limit |
.search(re)
|
str.search(regex) |
'hi123'.search(/\d/) |
2 | Returns index of first regex match; -1 if no match |
String — Transform
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
.slice(s,e)
|
str.slice(start, end) |
'hello'.slice(1,3) |
'el' | Extract substring; negative index counts from end |
.substring(s,e)
|
str.substring(s, e) |
'hello'.substring(1,3) |
'el' | Like slice but treats negatives as 0; no negative indexing |
.split(sep)
|
str.split(sep, lim) |
'a,b,c'.split(',') |
['a','b','c'] | Split into array; empty string splits every character |
.trim()
|
str.trim() |
' hi '.trim() |
'hi' | Remove leading and trailing whitespace |
.trimStart()
|
str.trimStart() |
' hi '.trimStart() |
'hi ' | Remove leading whitespace only |
.trimEnd()
|
str.trimEnd() |
' hi '.trimEnd() |
' hi' | Remove trailing whitespace only |
.replace(s,r)
|
str.replace(s, r) |
'foo'.replace('o','0') |
'f0o' | Replace first match; use regex /s/g for all occurrences |
.replaceAll(s,r)
|
str.replaceAll(s, r) |
'foo'.replaceAll('o','0') |
'f00' | Replace all occurrences (ES2021); also str.replace(/s/g, r) |
.toUpperCase()
|
str.toUpperCase() |
'hello'.toUpperCase() |
'HELLO' | Convert to uppercase; locale-independent |
.toLowerCase()
|
str.toLowerCase() |
'HELLO'.toLowerCase() |
'hello' | Convert to lowercase; locale-independent |
.padStart(n,s)
|
str.padStart(len, fill) |
'5'.padStart(3,'0') |
'005' | Pad start of string to target length with fill character |
.padEnd(n,s)
|
str.padEnd(len, fill) |
'hi'.padEnd(5,'.') |
'hi...' | Pad end of string to target length with fill character |
.repeat(n)
|
str.repeat(count) |
'ab'.repeat(3) |
'ababab' | Return string repeated n times |
.concat(...s)
|
str.concat(s1, s2) |
'a'.concat('b','c') |
'abc' | Concatenate strings; template literals are usually cleaner |
.at(i)
|
str.at(index) |
'hello'.at(-1) |
'o' | Access character by index; negative index counts from end (ES2022) |
String — Pattern Matching
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
.match(re)
|
str.match(regex) |
'a1b2'.match(/\d+/g) |
['1','2'] | Match regex; with /g returns all matches array, else first+groups |
.matchAll(re)
|
str.matchAll(regex) |
[...'a1'.matchAll(/\d/g)] |
iterator | Returns iterator of all matches with capture groups (ES2020); needs /g flag |
String.fromCharCode
|
String.fromCharCode(n) |
String.fromCharCode(65) |
'A' | Create string from UTF-16 char codes |
.charCodeAt(i)
|
str.charCodeAt(i) |
'A'.charCodeAt(0) |
65 | UTF-16 code unit at index i |
Array — Properties & Mutation
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
.length
|
arr.length |
[1,2,3].length |
3 | Number of elements; settable to truncate array |
.push(...v)
|
arr.push(v1, v2) |
arr.push(4) |
new length | Append elements to end; returns new length (mutates) |
.pop()
|
arr.pop() |
[1,2,3].pop() |
3 | Remove and return last element (mutates) |
.shift()
|
arr.shift() |
[1,2,3].shift() |
1 | Remove and return first element (mutates) |
.unshift(...v)
|
arr.unshift(v) |
arr.unshift(0) |
new length | Prepend elements to start; returns new length (mutates) |
.splice(s,d,...v)
|
arr.splice(start, del, ...items) |
arr.splice(1,0,'x') |
removed[] | Insert/remove at position; returns removed elements (mutates) |
.fill(v,s,e)
|
arr.fill(val, start, end) |
[0,0,0].fill(5,1) |
[0,5,5] | Fill elements with value between start and end (mutates) |
.reverse()
|
arr.reverse() |
[1,2,3].reverse() |
[3,2,1] | Reverse array in place (mutates) |
.sort(fn)
|
arr.sort(compareFn) |
arr.sort((a,b)=>a-b) |
sorted arr | Sort in place (mutates); always provide compareFn for numbers |
Array — Search & Test
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
.indexOf(v)
|
arr.indexOf(v, from) |
[1,2,3].indexOf(2) |
1 | First index of value; -1 if not found; uses strict equality |
.lastIndexOf(v)
|
arr.lastIndexOf(v) |
[1,2,1].lastIndexOf(1) |
2 | Last index of value; -1 if not found |
.includes(v)
|
arr.includes(v, from) |
[1,2,3].includes(2) |
true | True if value exists; works with NaN (unlike indexOf) |
.find(fn)
|
arr.find(predicate) |
arr.find(x=>x>2) |
element | First element where predicate returns true; undefined if none |
.findIndex(fn)
|
arr.findIndex(predicate) |
arr.findIndex(x=>x>2) |
index | Index of first match; -1 if none found |
.some(fn)
|
arr.some(predicate) |
arr.some(x=>x>5) |
true/false | True if at least one element passes predicate |
.every(fn)
|
arr.every(predicate) |
arr.every(x=>x>0) |
true/false | True only if ALL elements pass predicate |
Array — Transform
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
.map(fn)
|
arr.map(callback) |
[1,2,3].map(x=>x*2) |
[2,4,6] | New array with each element transformed (does not mutate) |
.filter(fn)
|
arr.filter(predicate) |
[1,2,3].filter(x=>x>1) |
[2,3] | New array with elements passing predicate (does not mutate) |
.reduce(fn,v)
|
arr.reduce(fn, init) |
[1,2,3].reduce((a,b)=>a+b,0) |
6 | Reduce to single value; provide initial value to avoid edge cases |
.forEach(fn)
|
arr.forEach(callback) |
arr.forEach(x=>log(x)) |
undefined | Execute function for each element; returns nothing (use map to transform) |
.flat(n)
|
arr.flat(depth) |
[[1],[2,3]].flat() |
[1,2,3] | Flatten nested arrays; depth defaults to 1, Infinity flattens all |
.flatMap(fn)
|
arr.flatMap(fn) |
[1,2].flatMap(x=>[x,x*2]) |
[1,2,2,4] | Map then flatten one level; more efficient than .map().flat() |
.slice(s,e)
|
arr.slice(start, end) |
[1,2,3,4].slice(1,3) |
[2,3] | Extract subarray; does not mutate; end index excluded |
.concat(...v)
|
arr.concat(arr2) |
[1].concat([2,3]) |
[1,2,3] | Merge arrays into new array; does not mutate |
.join(sep)
|
arr.join(separator) |
['a','b'].join('-') |
'a-b' | Join elements into string; default separator is comma |
Array — Static Methods & Iterators
| Method | Syntax | Example | Returns | Description |
|---|---|---|---|---|
Array.from(v)
|
Array.from(v, mapFn) |
Array.from('abc') |
['a','b','c'] | Create array from iterable or array-like; optional map function |
Array.isArray(v)
|
Array.isArray(v) |
Array.isArray([1,2]) |
true | True if value is an array; typeof returns 'object' so use this instead |
Array.of(...v)
|
Array.of(v1, v2) |
Array.of(1,2,3) |
[1,2,3] | Create array from arguments; unlike Array(3) which creates holes |
.entries()
|
arr.entries() |
for(let[i,v] of arr.entries()) |
iterator | Returns [index, value] pairs iterator |
.keys()
|
arr.keys() |
[...arr.keys()] |
[0,1,2,...] | Returns index iterator |
.values()
|
arr.values() |
[...arr.values()] |
elements | Returns value iterator; equivalent to iterating array directly |
.at(i)
|
arr.at(index) |
[1,2,3].at(-1) |
3 | Access element by index; negative counts from end (ES2022) |
.copyWithin(t,s)
|
arr.copyWithin(target, start) |
[1,2,3,4].copyWithin(0,2) |
[3,4,3,4] | Copy portion of array to another position within same array (mutates) |