JavaScript - Basic, Advanced, & More Cheat Sheet by Acwinter
JavaScript - Basic, Advanced, & More Cheat Sheet by Acwinter
the purpose of this cheat sheet is to briefly describe the core Math.ceil(num)
elements of the JavaScript language for those of studying it who • rounds num up to nearest integer
have taken in much more than we can hold onto well. nothing here is Math.floor(num)
explained in full but rather meant to get you on the right track. also, • rounds num down to nearest integer
this document purposely does not cover browser-specific methods / Math.max(num1, num2)
syntax / objects and the like. • returns larger num
this cheat sheet is a work in progress and may be updated -- Math.min(num1, num2)
check back on occasion! Math.pow(num1, num2)
• returns num1 to the power num2
Types Math.sqrt(num)
Type typeOf evaluation Primitive? Math.random()
Null object yes • returns decimal between 0 (inclusive) and 1(exclusive)
Math.abs(num)
Undefined undefined yes
• returns absolute value of num•
Boolean boolean yes
String string yes Array "Extra Methods"
Number number yes
Note: these "extra methods," which are "higher-order" functions,
Object object no -- an object ignore holes in the array (i.e.: [“apples”, , , , “oranges”]). they also
Function function no -- an object have more arguments than shown here -- best to look them up for
more info!
Array object no -- an object
Note: array-like objects, for example arguments and NodeLists,
Symbol symbol no -- an object
can also make use of these methods.
[] object no -- an object arr.some(callback)
{} object no -- an object arr.every(callback)
• returns a boolean value. returns true if some or every element in the
Number & Math Methods array meets the evaluation. example:
someNum.toFixed(num) var a = [1,2,3];
• shortens someNum to have only num decimal places var b = a.every(function(item){
num.toExponential() return item > 1;
• converts num to exponential notation (i.e. 5.569e+0) }); // false
num.toString() arr.reduce(function(prev, next){..}, startVa
• converts num to a string l)
num.toPrecision(#) arr.reduceRight(function(prev, next){..},
• converts num to a num with # places starting with whole numbers startVal)
String(someValue) • returns a value. reduce employs a callback to run through the
• converts or coerces someValue to a string - someValue can be any elements of the array, returning "prev" to itself with each iteration and
type, ie "Boolean(1)" returns true taking the next "next" value in the array. for it's first "prev" value it will
parseInt(string, radix) take an optional "startVal" if supplied. an interesting example:
parseFloat(string, radix) var arr = ["apple", "pear", "apple", "lem‐
• converts a string into an integer. the optional radix argument on"];
defines the base -- i.e., base 10 (decimal) or base 16 (hexadecimal). var c = arr.reduce(function(prev, next) {
Math.round(num) prev[next] = (prev[next] += 1) || 1;
• rounds num to nearest integer return prev;
}, {}); Callbacks: placing ( ) after a function call executes it immediately. leaving the
// objCount = { apple: 2, pear: 1, lemon: 1 } for a callback.
arr.filter(function(){..}) Function Declaration
function
• returns an array. filter returns an array of elements that satisfy a given callback. example: aFunctionName (args) {...
var arr2 = ["jim", "nancy", "ned"]; • functions created in this manner are evaluated when the code is parsed and a
var letter3 = arr2.filter(function(item) { the top and are available to the code even before they're formally declared. Not
return (item.length === 3); odd construction, using function declarations within a flow control statement can
Loops / Control Flow Statements (cont) String Methods, Properties & Etc (cont)
for .. in (objects) • returns substring starting at index1 and running num letters
• returns number of milliseconds since epoch Note: index numbers for arrays start at 0
parse() arr.length()
• returns milliseconds between date and Unix epoch. arr. push(val)
toDateString() • adds val to end of arr
toTimeString() arr. pop()
toLocalTimeString() • deletes last item in arr
arr. shift()
Get / Set Date Methods • deletes first item in arr
arr.unshift(val)
• getDate() • getHours()
• adds val to front of arr
• getDay() • getMilliseconds()
arr.reverse ()
• getFullYear() • getMinutes()
arr1.concat(arr2)
• getMonth() • getSeconds() • concatenates arr1 with arr2
• getTime() • getTimezoneOffset() arr.join(char)
• returns string of elements of arr joined by char
Note: there are also 'set' methods such as setMonth() .
arr.slice(index1, index2)
Note: getDay and getMonth return numeric representations
starting with 0. • returns a new array from arr from index1 (inclusive) to index2
(exclusive)
Chaining Method
also known as cascading, refers to repeatedly calling one method an object property has a function for its value.
after another on an object, in one continuous line of code.
where two or more identifiers in a given namespace or a given abstract arguments boolean break
scope cannot be unambiguously resolved byte case catch char
DRY class const continue debugger
Don't Repeat Yourself default delete do double
(also ECMA-262) the specification from which the JavaScript extends false final finally
implementation is derived. version 5.1 is the current release. float for function goto
Arity if implements import in
refers to the number of arguments an operator takes. ex: a binary instanceof int interface let
function takes two arguments long native new null
Currying package private protected public
refers to the process of transforming a function with multiple arity return short static super
into the same function with less arity
switch synchronized this throw
Recursion throws transient true try
an approach in which a function calls itself typeof var void volatile
Predicate while with yield
a calculation or other operation that would evaluate either to "tru‐
e" or “false.” Prototype-based Inheritance
Callback Hell
code thickly nested with callbacks within callback within callbacks.
Closure
a function with access to the global scope, it's parent scope (if
there is one), and it's own scope. a closure may retain those
scopes even after it's parent function has returned.
IIFE
Immediately Invoked Function Expressions. pronounced "iffy." a
function that is invoked immediately upon creation. employs a
unique syntax.