JavaScript Array, String and RegExp Cheat Sheet
by LeiQ (amethystlei) via cheatography.com/27322/cs/7820/
Create an Array Array instance mutator methods (cont)
By literal way start Index at which to start changing the array. If greater than
[element0, element1, ..., elementN] the length, will set to length; if negative, will begin from the
new Array(element0, element1[, ...[, elementN]]) end.
new Array(arrayLength) dele‐ If 0, will not delete any element. If Omitted, will be equal to
By using the result of a math teC‐ length - start
ount
an array is returned by
- RegExp.exec item1, The elements to add to the array
- String.match item2
- String.replace returns an array containing the deleted elements
By using Methods These methods modify the array
Array.from(arrayLike[, mapFn[, thisArg]])
Array instance Accessor methods
an array-like object (object with a length property and indexed
elements, such as arguements) or iterable object (object where concat var new_array = old_array.concat( value1[, value2[, ...
you can get its elements, such as Map and Set). valueN]])
Array.of(element0[, element1[, ...[, elementN] ]]) slice var shallo
w_c opy = arr.slice([begin[, end]])
Every argument is considered as an element in the array. begin Zero-based. If negative, indicate an offset from
the end
Array.from and Array.of work like Array constructor to create
join str = arr.join([separator = ','])
an new array.
indexOf index = arr.indexOf(searchElement[, fromIndex = 0])
Array instance mutator methods lastIn‐ index = arr.indexOf(searchElement[, fromIndex =
copyWithin( target, start[, end = this.length]) dexOf arr.length - 1])
target Target start index
Array instance Iteration methods
start Source start index, if it is negative, treated as length
forEach
+ start
map return new array
fill(value[, start = 0[, end = this.length]])
filter return new array
value Value to fill an array
every return true if every element satisfies testing function
sort([compareFunction])
some return true if at least one element satisfies testing function
function compare(a, b) {
if (a less than b, or a should be in front of b) {return -1;} find return the found value or undefined
if (a is greater than b or be behind of b) {return 1;}
return 0; // a must be equal to b
}
splice(start, deleteCount[, item1[, item2[, ...]]])
By LeiQ (amethystlei) Published 30th September, 2020. Sponsored by Readable.com
Last updated 30th September, 2020. Measure your website readability!
Page 1 of 3. https://readable.com
cheatography.com/amethystlei/
JavaScript Array, String and RegExp Cheat Sheet
by LeiQ (amethystlei) via cheatography.com/27322/cs/7820/
Array instance Iteration methods (cont) String instance methods unrelated to HTML
findIndex return the found Index or -1 concat str.concat( string2, string3[, ..., stringN])
callback[, thisArg] repeat str.repeat(count)
callback currentValue count will convert to integer
'abc'.repeat(2) -> 'abcabc'
index
includes str.funcName( searchString[, position])
array
endsWith
reduce accumulate startsWith
reduceRight accumulate from end searchS‐ A string to be searched for within this
callback[, initialValue] tring string
callback previousValue position search from, optional
currentValue case-sensitivity, return true or false
currentIndex
String instance methods related with RegExp
array
entries key/value pairs search str.search( regexp)
keys keys return the index of first match or -1
values values match str.match(regexp)
same as regexO
bj. exec(str)
return an new Array Iterator object
return an array containing the entire match result or null
Some of them can also use for array-like objects by Array.prototy‐
result: input, index, 0, 1-n
pe.fun.call(array-like object, args)
replace str.replace(regexp |substr, newSubStr|fun
ction)
All the Array instance methods regexp pattern
copyWithin fill pop push substr
reverse shift sort splice newSubStr replacement
function
unshift concat join slice
newSubStr can include some special
toString toLocaleString indexOf lastIndexOf
replacement patterns
forEach entries every some
$$ inserts a '$'
filter find findIndex keys
map reduce reduceRight values
Create a string
String literals
'string text'
"string text"
String( text)
`string text ${variable}` template strings
Create by Char codes
String.fromCharCode(num1[, ...[, numN]])
By LeiQ (amethystlei) Published 30th September, 2020. Sponsored by Readable.com
Last updated 30th September, 2020. Measure your website readability!
Page 2 of 3. https://readable.com
cheatography.com/amethystlei/
JavaScript Array, String and RegExp Cheat Sheet
by LeiQ (amethystlei) via cheatography.com/27322/cs/7820/
String instance methods related with RegExp (cont) Escape notation
$& inserts the matched substring \0 the NULL character
$` inserts the portion of the string that precedes the matched \' single quote
string \" double quote
$' inserts the portion of the string that follows the matched \\ backslash
substring
\n new line
$n or n and nn are decimal digits, inserts the nth parenthesized
\r carriage return
$nn submatch string
\v vertical tab
function's result will be used as the replacement, and the
\t tab
arguments is:
\b backspace
match like $&
\f form feed
p1, p2, like $n
... \uXXXX unicode codepoint
offset The offset of the matched substring within the whole string \xXX the Latin-1 character
string the whole string
Create a RegExp
String instance methods related HTML literal notation
anchor str.anchor( name) /pattern/ flags
create and display an anchor (name property) in a constructor
document new RegExp(pattern[, flags])
e.g. <a name="name">str</a>
pattern The text
link str.link(url) flags
create an HTML snippet for a hypertext link g global match
e.g. <a href="url">str</a>
i ignore case
m multiline
All the String instance methods
use test to test for a match in its string parameter.
charAt charCodeAt concat includes
endsWith indexOf lastIndexOf locale‐
Compare
match repeat replace search
slice split startsWith substr
substring toLocaleLowe‐ toLocaleUppe‐ toLowerCase
rCase rCase
toString toUpperCase trim valueOf
anchor link
By LeiQ (amethystlei) Published 30th September, 2020. Sponsored by Readable.com
Last updated 30th September, 2020. Measure your website readability!
Page 3 of 3. https://readable.com
cheatography.com/amethystlei/