a custom collection of JavaScript commands, syntax, ideas, and tips
|
Type
|
typeOf evaluation
|
Primitive?
|
|
Null
|
object
|
yes
|
|
Undefined
|
undefined
|
yes
|
|
Boolean
|
boolean
|
yes
|
|
String
|
string
|
yes
|
|
Number
|
number
|
yes
|
|
Object
|
object
|
no --an object
|
|
Function
|
function
|
no --an object
|
|
Array
|
object
|
no --an object
|
|
Symbol
|
symbol
|
no --an object
|
|
[]
|
object
|
no --an object
|
|
{}
|
object
|
no --an object
|
someNum.toFixed(num)num.toExponential()num.toString()num.toPrecision(#)String(someValue)parseInt(string, radix)parseFloat(string, radix)Math.round(num)Math.ceil(num)Math.floor(num)Math.max(num1, num2)Math.min(num1, num2)Math.pow(num1, num2)Math.sqrt(num)Math.random()Math.abs(num)arguments and NodeLists, can also make use of these methods.arr.some(callback)arr.every(callback)var a = [1,2,3];var b = a.every(function(item){ return item > 1;}); // falsearr.reduce(function(prev, next){..}, startVal)arr.reduceRight(function(prev, next){..}, startVal)var arr = ["apple", "pear", "apple", "lemon"];var c = arr.reduce(function(prev, next) {prev[next] = (prev[next] += 1) || 1;return prev;}, {});// objCount = { apple: 2, pear: 1, lemon: 1 }arr.filter(function(){..})var arr2 = ["jim", "nancy", "ned"];var letter3 = arr2.filter(function(item) {return (item.length === 3);});console.log(letter3); // ['jim', 'ned']arr.sort(function(){..})var stock = [{key: “r”, num: 12}, {key: “a”, num: 2}, {key: “c”, num: 5}];var c = stock.sort(function(a,b) { return a.num - b.num;} ); // [ { key: ‘a', num: 2 }, { key: ‘c', num: 5 }, { key: ‘r', num: 12 } ]arr.map()var stock = [{key: "red", num: 12}, {key: "blue", num: 2}, {key: "black", num: 2}];var b = stock.map(function (item){return item.key;}) // ["red","blue","black"]arr.forEach()var arr = [“jim”, “mary”];a.forEach (function (item) { console.log("I simply love “ +item);}); // “I simply love jim”, “I simply love mary"array.sort().reverse()... function aFunctionName (args) {... var bar = function (args) {... var bar = function foo (args) {... var anotherFunction = new Function (args, function () {... }) {...} ( function (args) { doSomething; } ) ( );if (considtion1) { doSomething;} else if { doSomethingElse;} else { doSomethingMore;}for (var i = 0; i < someNumber; i++) { doSomething;}switch (someEvaluation) { case "evaluatesAsThis" : doSomething; case "evaluatesAsThat" : doSomethingElse;}while (someEvaluation === true) { doSomething;}do { doSomething;}while (someEvaluation === true);for (anItem in anObject) { doSomething With anItem; // will be the key doSomethingWith Object[anItem]; // will be the value of that key}str.charAt(num) str.charCodeAt(num)
• returns the character with unicode's num
str.indexOf(char)
• returns -1 if char not found in str
str.lastIndexOf(subString)
• returns the index of the last occurrence of subString
str.length
• returns length of str starting at 1
str.match(pattern)
• returns null if not found. returns an array of all matches
str.match(/pattern/g)
• provides global search of string
str.replace(old, new)
str.search(pattern)
• returns index of first match or -1 if not found
str.substring(index1, index2)
• char at index1 is returned, index2 is not
str.split(char)
• returns an array of str split on char
str.substr(index1, num)
• returns substring starting at index1 and running num letters
str.toLowerCase()
str.toUpperCase()
str.toLocaleLowerCase()
• takes local language settings into account
str.toLocaleUpperCase()
• ibid
Number(var/string/object)
• converts to number. "true" converts to 1, etc
one.concat(two)
• concatenates string/array one with two
JSON.stringify( )
• converts a javascript value/object into a string
JSON.parse ( )`var today = new Date();var someDate = new Date("june 30, 2035");var today = Date.now();parse()toDateString()toTimeString()toLocalTimeString()|
• getDate()
|
• getHours()
|
|
• getDay()
|
• getMilliseconds()
|
|
• getFullYear()
|
• getMinutes()
|
|
• getMonth()
|
• getSeconds()
|
|
• getTime()
|
• getTimezoneOffset()
|
break;continue;isNaN(someVar)isFinite(someVar)var aVar = anObject[anAttribute] || "nonesuch";var aVar = anEvaluation ? trueVal : falseVal;delete anObject[anAttribute](aProperty in anObject)eval(someString)arr.length()arr. push(val)arr. pop()arr. shift()arr.unshift(val)arr.reverse ()arr1.concat(arr2)arr.join(char)arr.slice(index1, index2)arr.splice(index, num, itemA, itemB,..)|
Higher Order Functions
functions that accept other functions as an argument
|
|
Scope
the set of variables, objects, and functions available within a certain block of code
|
|
Callback
(also event handler) a reference to executable code, or a piece of executable code, that is passed as an argument to other code.
|
|
the % operator
% returns the remainder of a division such that "3 % 2 = 1" as 2 goes into 3 once leaving 1. called the "remainder" or "modulo" operator.
|
|
Composition
the ability to assemble complex behaviour by aggregating simpler behavior. chaining methods via dot syntax is one example.
|
|
Chaining
also known as cascading, refers to repeatedly calling one method after another on an object, in one continuous line of code.
|
|
Naming Collisions
where two or more identifiers in a given namespace or a given scope cannot be unambiguously resolved
|
|
DRY
Don't Repeat Yourself
|
|
ECMAScript
(also ECMA-262) the specification from which the JavaScript implementation is derived. version 5.1 is the current release.
|
|
Arity
refers to the number of arguments an operator takes. ex: a binary function takes two arguments
|
|
Currying
refers to the process of transforming a function with multiple arity into the same function with less arity
|
|
Recursion
an approach in which a function calls itself
|
|
Predicate
a calculation or other operation that would evaluate either to "true" or “false.”
|
|
Asynchronous
program flow that allows the code following an asynchronous statement to be executed immediately without waiting for it to complete first.
|
|
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.
|
|
Method
an object property has a function for its value.
|
|
abstract
|
arguments
|
boolean
|
break
|
|
byte
|
case
|
catch
|
char
|
|
class
|
const
|
continue
|
debugger
|
|
default
|
delete
|
do
|
double
|
|
else
|
enum
|
eval
|
export
|
|
extends
|
false
|
final
|
finally
|
|
float
|
for
|
function
|
goto
|
|
if
|
implements
|
import
|
in
|
|
instanceof
|
int
|
interface
|
let
|
|
long
|
native
|
new
|
null
|
|
package
|
private
|
protected
|
public
|
|
return
|
short
|
static
|
super
|
|
switch
|
synchronized
|
this
|
throw
|
|
throws
|
transient
|
true
|
try
|
|
typeof
|
var
|
void
|
volatile
|
|
while
|
with
|
yield
|
|
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.