Built in Objects & Functions - SelfishHellfish/JS_Ref GitHub Wiki
built-in methods and functions that work on the window object/global scope/object.prototype()
timers and intervals
var interval = setInterval(function() {console.log('test');}, 500);
setTimeout(function() {clearInterval(interval);}, 4000)
...will print out 'test' 8 times in 4 seconds.
transforming/parsing formats and values
parseInt()
will attempt to transform a string into a number. parseInt('FBB123', 16)
will get the integer for the hexadecimal value 'FBB123'
a.toString()
will attempt to transform the value stored in a
to a string
a.toFixed(2)
can be used to round e.g. 10.4
to two decimal places --> 10.40
string functions
strings are treated like arrays
string.length()
to get string length, string[index]
or string.charAt(index)
to access string character, string.concat(' text to append to string'),
string.toUpperCase(),
string.split(' '),
string.trim()` to strip white spaces to the left and right of non-space character boundaries.
math object
Math.PI
, Math.E
, Math.abs(-5)
, Math.round(1.4)
, Math.ceil(1.3)
to round up, Math.floor(1.6)
to round down, Math.exp(num)
== e^num, Math.log(Math.E)
== 1, Math.max(1,100,1000)
or Math.min(1,100,1000) to get highest/lowest value in list,
Math.random()` for random number between 0 and 1
date object
create custom dates with console.log(new Date('2016/05/23').toString())
or print current date with Date.toString()
to get the number of ms since Jan. 1, 1970, do Date.parse('2016/05/23')
Date.getDay
for day of week, Date.getDate
for day of month
regular expressions
pattern.exec(string)
/string.match(pattern)
to search expression or pattern.test(string)
to evaluate if expression exists, where pattern = /abc/;
cheat sheet