Acwinter Javascript Basic Advanced and More
Acwinter Javascript Basic Advanced and More
the purpose of this cheat sheet is to briefly describe the core elements of Math.ceil(num)
the JavaScript language for those of studying it who have taken in much rounds num up to nearest integer
more than we can hold onto well. nothing here is explained in full but rather Math.floor(num)
meant to get you on the right track. also, this document purposely does
rounds num down to nearest integer
not cover browser-specific methods / syntax / objects and the like.
Math.max(num1, num2)
this cheat sheet is a work in progress and may be updated -- check
returns larger num
back on occasion!
Math.min(num1, num2)
Math.pow(num1, num2)
Types
returns num1 to the power num2
Type typeOf evaluation Primitive? Math.sqrt(num)
Array object no --an object arguments than shown here -- best to look them up for more info!
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 ifsome or every element in the
Number & Math Methods array meets the evaluation. example:
var a = [1,2,3];
someNum.toFixed(num)
var b = a.every(function(item){
shortens someNum to have only num decimal places
num.toExponential() return item > 1;
converts or coerces someValue to a string - someValue can be any type, optional "startVal" if supplied. an interesting example:
converts a string into an integer. the optional radix argument defines the return prev;
base -- i.e., base 10 (decimal) or base 16 (hexadecimal).
Math.round(num)
rounds num to nearest integer
returns an array. filter returns an array of elements that satisfy a given function aFunctionName (args) {...
callback. example: functions created in this manner are evaluated when the code is parsed
var arr2 = ["jim", "nancy", "ned"]; and are 'hoisted' to the top and are available to the code even before
var letter3 = arr2.filter(function(item) { they're formally declared. Note: Due to JS's odd construction, using
function declarations within a flow control statement can be wonky and is
return (item.length === 3);
best avoided.
}
);
Function Expression / Anonymous Functions
c
onsole.log(letter3); // ['jim', 'ned']
var bar = function (args) {...
arr.sort(function(){..})
(also referred to as 'Function Operators') anonymous functions are
returns the original array, mutated. sort returns the elements sorted with evaluated at 'runtime' and are therefore less memory intensive. they must
a given criteria. for example: be provided a variable name but need not have a function name
var stock = [{key: r, num: 12}, {key: a, num: (therefore: anonymous). [these are ]
2}, {key: c, num: 5}]; Named Function Expression
var c = stock.sort(function(a,b) { var bar = function foo (args) {...
return a.num - b.num; confusingly, this is still an 'anonymous function.' assigning a name is
} ); // [ { key: a', num: 2 }, { key: c', num: 5 useful for debugging purposes and also allows for self-referential /
recursive calls
}, { key: r', num: 12 } ]
Function Constructor
arr.map()
var anotherFunction = new Function (args, function
returns an array. map goes over every element in the array, calls a
() {... }) {...}
callback on the element, and sets an element in the new array to be equal
to the return value the callback. for example: equivalent to a functional expression
var stock = [{key: "red", num: 12}, {key: "blue", Self-Invoking Anonymous Functions
( function (args) { doSomething; } ) ( );
num: 2}, {key: "black", num: 2}];
(also known as IIFEs / 'Immediately Invoked Function Expressions') and
var b = stock.map(function (item){
invokes immediately
return item.key;
}) // ["red","blue","black"]
Loops / Control Flow Statements
arr.forEach()
if .. else if .. else
no return value. forEach performs an operation on all elements of the
array. for example: if (considtion1) {
Loops / Control Flow Statements (cont) String Methods, Properties & Etc (cont)
"this" one.concat(two)
concatenates string/array one with two
coming soon JSON.stringify( )
converts a javascript value/object into a string
String Methods, Properties & Etc JSON.parse ( )
a string can be coerced into an array so many array methods are converts a JSON string into a javascript object
applicable as well
Date Methods
str.charAt(num)
Note: Unix epoch is January 1, 1970
returns the character in str at index num
var today = new Date();
str.charCodeAt(num)
creates date object for now
returns the unicode value of the char
var someDate = new Date("june 30, 2035");
String.fromCharCode(num)`
creates date object for arbitrary date
returns the character with unicode's num
var today = Date.now();
str.indexOf(char)
returns -1 if char not found in str
str.lastIndexOf(subString)
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()
break; itemsA..
stops current loop iteration and increments to next Higher Order Functions
isNaN(someVar)
functions that accept other functions as an argument
returns true if not a number
isFinite(someVar) Scope
var aVar = anObject[anAttribute] || "nonesuch"; the set of variables, objects, and functions available within a certain
assigns a default value if none exists block of code
var aVar = anEvaluation ? trueVal : falseVal;
Callback
ternary operator. assigns trueVal to aVar if anEvaluation is true, falseVal
(also event handler) a reference to executable code, or a piece of
if not
executable code, that is passed as an argument to other code.
delete anObject[anAttribute]
(aProperty in anObject) the % operator
returns true or false if aProperty is a property of anObject % returns the remainder of a division such that "3 % 2 = 1" as 2 goes
eval(someString) into 3 once leaving 1. called the "remainder" or "modulo" operator.
evaluates a someString as if it was JavaScript. i.e. eval("var x = 2+3")
Composition
returns 5
the ability to assemble complex behaviour by aggregating simpler
behavior. chaining methods via dot syntax is one example.
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 scope abstract arguments boolean break
cannot be unambiguously resolved byte case catch char
DRY class const continue debugger
(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
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 into return short static super
the same function with less arity switch synchronized this throw
Recursion throws transient true try
Callback Hell
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