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.forEa­ch(­fun­cti­on(val, I, arr))
Executes a function for each value.
arr.every­(fu­nct­ion­(val, i, arr))
Returns true if every value passes the function test
arr.reverse()
Reverses the original array
Searching
arr.inclu­des­(val)
Returns true if array contains a value
arr.index­Of(val)
Returns the index of a value in an array or -1 if not found
arr.find(­fun­ction)
Returns the value of the first index to satisfy the function
arr.findI­nde­x(f­unc­tion)
Returns the index of the first value to satisfy the function
Add Values
arr.splic­e(s­tart, 0, items...)
Adds items to array beginning at starting index
arr.unshi­ft(val)
Adds val to front of array
arr.push(val)
Adds val to back of array
Delete Values
arr.splice(s­tart, n)
Delete n items from array beginning at starting index
arr.shift()
Removes arr[0] and returns the value
arr.pop()
Removes last value in a array and returns it
Copy Values
arr.slice(beg, end)
Returns a shallow copy of arr from beg to end (non-i­ncl­usive)
To String
arr.join(separator)
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.toStr­ing()
Returns a comma-­del­imited string of scalar array values. Shallow string.
Sorting
arr.sort()
Sorts lexico­gra­phi­cally
arr.sort((a, b) => a - b)
Sorts numbers

Number Methods

num.toFixed(n)
Returns a number rounded to n signif­icant figures after the decimal
num.toPre­cision(n)
Returns a string rounded to n signif­icant figures
num.toStr­ing()
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 Properties
delete obj[key]
Delete property
obj[key] = value
Add property

String methods

str.slice(beg, end)
returns a substring from beginning to end index
str.toLow­erC­ase()
Returns a lowercase version of str
String to Array
str.split­(se­par­ator)
Returns an array of the string, split by the separator
Searching Strings
str.inclu­des­(str)
Returns true if str is in string
str.index­Of(str)
Returns index of str if within string, or -1 otherwise
str.start­sWi­th(str, beg)
Returns true if string starts with str, optionally from beg index.
str.endsW­ith­(str, len)
Returns true if string starts with str, optionally limited to the first len characters
Combine Strings
str.conca­t(s­tri­ngs...)
Returns new string concat­enation

Math Object Methods

Math.abs(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.
Rounding
Math.f­loor(num)
Goes down to the nearest int <= num
Math.ceil(num)
Goes up to the nearest int >= num
Math.r­ound(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

          CoffeeScript language Cheat Sheet
          OneStop(Nerd)Shop Cheat Sheet
          Base JavaScript Cheat Sheet