Essential Vanilla JavaScript Methods Cheat Sheet
by bytecut via [Link]/61737/cs/15924/
Iterating Array Methods (cont)
for (key in obj) Iterates over keys To String
for (value of obj) Iterates over values arr.join(separat Returns a string from the array values, with an optional
or) separator string
Array Methods JSON.stringify(a Returns a JSON string representation of array. Deep
rr) string.
arr.forEach(function(val, Executes a function for each value.
I, arr)) arr.toString() Returns a comma-delimited string of scalar array
values. Shallow string.
arr.every(function(val, i, Returns true if every value passes the function
arr)) test Sorting
arr.reverse() Reverses the original array arr.sort() Sorts lexicographically
Searching arr.sort((a, b) Sorts numbers
=> a - b)
arr.includes(val) Returns true if array contains a value
arr.indexOf(val) Returns the index of a value in an array or -1 if
Number Methods
not found
arr.find(function) Returns the value of the first index to satisfy the num.toFixed(n Returns a number rounded to n significant figures after
) the decimal
function
arr.findIndex(function) Returns the index of the first value to satisfy the num.toPrecisio Returns a string rounded to n significant figures
function n(n)
Add Values num.toString() Returns a string representation of a number
arr.splice(start, 0, Adds items to array beginning at starting index
Object Methods
items...)
arr.unshift(val) Adds val to front of array Object to Array
arr.push(val) Adds val to back of array [Link]ys(obj) Array of obj keys
Delete Values [Link]lues(obj) Array of obj values
arr.splice(start, n) Delete n items from array beginning at starting [Link]tries(obj) 2D array of key:value pairs
index Object to String
arr.shift() Removes arr[0] and returns the value JSON.stringify(obj) obj to string
arr.pop() Removes last value in a array and returns it JSON.parse(obj) string to obj
Copy Values Add/Remove Properties
arr.slice(beg, end) Returns a shallow copy of arr from beg to end delete obj[key] Delete property
(non-inclusive)
obj[key] = value Add property
By bytecut Published 29th May, 2018. Sponsored by [Link]
[Link]/bytecut/ Last updated 29th May, 2018. Learn to solve cryptic crosswords!
Page 1 of 2. [Link]
Essential Vanilla JavaScript Methods Cheat Sheet
by bytecut via [Link]/61737/cs/15924/
String methods
str.slice(beg, returns a substring from beginning to end index
end)
str.toLowerCas Returns a lowercase version of str
e()
String to Array
str.split(separat Returns an array of the string, split by the separator
or)
Searching Strings
str.includes(str) Returns true if str is in string
str.indexOf(str) Returns index of str if within string, or -1 otherwise
str.startsWith(st Returns true if string starts with str, optionally from beg
r, beg) index.
str.endsWith(str Returns true if string starts with str, optionally limited to
, len) the first len characters
Combine Strings
str.concat(string Returns new string concatenation
s...)
Math Object Methods
Math.abs(num) Returns the absolute value of num
Random Numbers
Math.round(M[Link]ndom() * (max - Returns a random number
min)) + min) between two ints
Math.random() Returns a random number
between 0 and 1.
Rounding
Math.floor(num) Goes down to the nearest int <=
num
Math.ceil( num) Goes up to the nearest int >=
num
Math.round(num) Rounds to the nearest int
By bytecut Published 29th May, 2018. Sponsored by [Link]
[Link]/bytecut/ Last updated 29th May, 2018. Learn to solve cryptic crosswords!
Page 2 of 2. [Link]