Appendix A — JavaScript Built-in Functions
This appendix provides a quick reference for JavaScript’s built-in functions that are commonly used in psychology experiments. Each function includes a definition and example usage.
A.1 Math Functions
JavaScript’s Math object provides mathematical constants and functions.
A.1.1 Math.round
Math.round(x)
- Rounds a number to the nearest integer
A.1.2 Math.floor
Math.floor(x)
- Rounds down to the nearest integer
A.1.3 Math.ceil
Math.ceil(x)
- Rounds up to the nearest integer
A.1.4 Math.abs
Math.abs(x)
- Returns the absolute value
A.1.5 Math.random
Math.random()
- Returns a random number between 0 (inclusive) and 1 (exclusive)
A.1.6 Math.min
Math.min(...values)
- Returns the smallest value
A.1.7 Math.max
Math.max(...values)
- Returns the largest value
A.2 Date Functions
JavaScript’s Date object handles dates and times.
A.2.1 New Date
new Date()
- Creates a new date object with current date/time
new Date(year, month, day)
- Creates a specific date (month is 0-indexed)
A.2.2 Date.now
Date.now()
- Returns current timestamp in milliseconds
A.3 String Functions
A.3.1 length
length
- Property that returns string length
A.3.2 toUpperCase
toUpperCase()
- Converts to uppercase
A.3.3 toLowerCase
toLowerCase()
- Converts to lowercase
A.3.4 charAt
charAt(index)
- Returns character at specified index
A.3.5 indexOf
indexOf(searchString)
- Returns first index of substring (-1 if not found)
A.3.6 slice
slice(start, end)
- Extracts part of string
A.3.7 replace
replace(searchValue, replaceValue
) - Replaces first occurrence
A.3.8 replaceAll
replaceAll(searchValue, replaceValue)
- Replaces all occurrences
A.3.9 split
split(separator)
- Splits string into array
A.3.10 trim
trim()
- Removes whitespace from both ends
A.3.11 includes
includes(searchString)
- Tests if string contains substring
A.3.12 startsWith
startsWith(searchString)
- Tests if string starts with substring
A.3.13 endsWith
endsWith(searchString)
- Tests if string ends with substring
A.4 Array Functions
A.4.1 length
length
- Property that returns array length
A.4.2 push
push(element)
- Adds element to end, returns new length
A.4.3 pop
pop()
- Removes and returns last element
A.4.4 unshift
unshift(element)
- Adds element to beginning
A.4.5 shift
shift()
- Removes and returns first element
A.4.6 indexOf
indexOf(searchElement)
- Returns first index of element (-1 if not found)
A.4.7 includes
includes(searchElement)
- Tests if array contains element
A.4.8 find
find(callback)
- Returns first element that matches condition
A.4.9 findIndex
findIndex(callback)
- Returns index of first element that matches condition
A.4.10 slice
slice(start, end)
- Returns shallow copy of portion of array
A.4.11 splice
splice(start, deleteCount, ...items)
- Changes array by removing/adding elements
A.4.12 concat
concat(array) - Combines arrays
A.4.13 join
join(separator)
- Joins array elements into string
A.4.14 reverse
reverse()
- Reverses array in place
A.4.15 sort
sort(compareFunction)
- Sorts array in place
A.4.16 forEach
forEach(callback)
Executes function for each element
A.4.17 map
map(callback)
- Creates new array with results of calling function on each element
A.4.18 filter
filter(callback)
- Creates new array with elements that pass test
A.4.19 reduce
reduce(callback, initialValue)
- Reduces array to single value
A.4.20 some
some(callback)
- Tests if at least one element passes test
A.4.21 every
every(callback)
- Tests if all elements pass test
A.5 Number Functions
A.5.1 parseInt
parseInt(string, radix)
- Parses string and returns integer
A.5.2 parseFloat
parseFloat(string)
- Parses string and returns floating point number
A.5.3 isNaN
isNaN(value)
- Tests if value is NaN (Not a Number)
A.5.4 Number
Number(value)
- Converts value to number
A.6 Object Functions
A.6.1 keys
Object.keys(obj)
- Returns array of object’s property names
A.6.2 values
Object.values(obj)
- Returns array of object’s property values
A.6.3 entries
Object.entries(obj)
- Returns array of [key, value] pairs