Show Menu
Cheatography

Essential Vanilla JavaScript Methods Cheat Sheet by

Iterating

for (key in obj)
Iterates over keys
for (value of obj)
Iterates over values

Array Methods

arr.f­or­Eac­h(f­unc­tio­n(val, I, arr))
Executes a function for each value.
arr.e­ve­ry(­fun­cti­on(val, i, arr))
Returns true if every value passes the function test
arr.r­ev­erse()
Reverses the original array
Sear­ching
arr.i­nc­lud­es(val)
Returns true if array contains a value
arr.i­nd­exO­f(val)
Returns the index of a value in an array or -1 if not found
arr.f­in­d(f­unc­tion)
Returns the value of the first index to satisfy the function
arr.f­in­dIn­dex­(fu­nction)
Returns the index of the first value to satisfy the function
Add Values
arr.s­pl­ice­(start, 0, items...)
Adds items to array beginning at starting index
arr.u­ns­hif­t(val)
Adds val to front of array
arr.p­us­h(val)
Adds val to back of array
Delete Values
arr.s­­p­lic­e(s­tart, n)
Delete n items from array beginning at starting index
arr.s­hift()
Removes arr[0] and returns the value
arr.p­op()
Removes last value in a array and returns it
Copy Values
arr.s­li­ce(­beg, end)
Returns a shallow copy of arr from beg to end (non-i­ncl­usive)
To String
arr.j­oi­n(­sep­ara­tor)
Returns a string from the array values, with an optional separator string
JSON.s­tri­ngi­fy(arr)
Returns a JSON string repres­ent­ation of array. Deep string.
arr.t­oS­tring()
Returns a comma-­del­imited string of scalar array values. Shallow string.
Sort­ing
arr.s­ort()
Sorts lexico­gra­phi­cally
arr.s­or­t((a, b) => a - b)
Sorts numbers

Number Methods

num.t­oF­ixe­d(n)
Returns a number rounded to n signif­icant figures after the decimal
num.t­oP­rec­isi­on(n)
Returns a string rounded to n signif­icant figures
num.t­oS­tring()
Returns a string repres­ent­ation of a number
 

Object Methods

Object to Array
Object.ke­ys(obj)
Array of obj keys
Object.va­lue­s(obj)
Array of obj values
Object.en­tri­es(obj)
2D array of key:value pairs
Object to String
JSON.s­tri­ngi­fy(obj)
obj to string
JSON.p­ars­e(obj)
string to obj
Add/­Remove Proper­ties
delete obj[key]
Delete property
obj[key] = value
Add property

String methods

str.s­li­ce(­beg, end)
returns a substring from beginning to end index
str.t­oL­owe­rCase()
Returns a lowercase version of str
String to Array
str.s­pl­it(­sep­arator)
Returns an array of the string, split by the separator
Sear­ching Strings
str.i­nc­lud­es(str)
Returns true if str is in string
str.i­nd­exO­f(str)
Returns index of str if within string, or -1 otherwise
str.s­ta­rts­Wit­h(str, beg)
Returns true if string starts with str, optionally from beg index.
str.e­nd­sWi­th(str, len)
Returns true if string starts with str, optionally limited to the first len characters
Combine Strings
str.c­on­cat­(st­rin­gs...)
Returns new string concat­enation

Math Object Methods

Math.a­bs(­num)
Returns the absolute value of num
Random Numbers
Math.r­oun­d(M­ath.ra­ndom() * (max - min)) + min)
Returns a random number between two ints
Math.r­andom()
Returns a random number between 0 and 1.
Roun­ding
Math.f­loo­r(­num)
Goes down to the nearest int <= num
Math.c­eil­(num)
Goes up to the nearest int >= num
Math.r­oun­d(­num)
Rounds to the nearest int
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          JavaScript Cheat Sheet
          CoffeeScript language Cheat Sheet
          C Reference Cheat Sheet