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

Math.round(4.7);  // 5
Math.round(4.3);  // 4

A.1.2 Math.floor

Math.floor(x) - Rounds down to the nearest integer

Math.floor(4.9);  // 4
Math.floor(-4.1); // -5

A.1.3 Math.ceil

Math.ceil(x) - Rounds up to the nearest integer

Math.ceil(4.1);   // 5
Math.ceil(-4.9);  // -4

A.1.4 Math.abs

Math.abs(x) - Returns the absolute value

Math.abs(-5);     // 5
Math.abs(3);      // 3

A.1.5 Math.random

Math.random() - Returns a random number between 0 (inclusive) and 1 (exclusive)

Math.random();                           // 0.7834592847
Math.floor(Math.random() * 10);         // Random integer 0-9
Math.floor(Math.random() * 5) + 1;      // Random integer 1-5

A.1.6 Math.min

Math.min(...values) - Returns the smallest value

Math.min(1, 3, 2);        // 1
Math.min(...[4, 2, 8]);   // 2 (using spread operator with array)

A.1.7 Math.max

Math.max(...values) - Returns the largest value

Math.max(1, 3, 2);        // 3
Math.max(...[4, 2, 8]);   // 8

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

const now = new Date();
console.log(now); // Current date and time

new Date(year, month, day) - Creates a specific date (month is 0-indexed)

const specificDate = new Date(2025, 0, 15); // January 15, 2025

A.2.2 Date.now

Date.now() - Returns current timestamp in milliseconds

const timestamp = Date.now(); // 1691234567890

A.3 String Functions

A.3.1 length

length - Property that returns string length

"hello".length; // 5

A.3.2 toUpperCase

toUpperCase() - Converts to uppercase

"hello".toUpperCase(); // "HELLO"

A.3.3 toLowerCase

toLowerCase() - Converts to lowercase

"HELLO".toLowerCase(); // "hello"

A.3.4 charAt

charAt(index) - Returns character at specified index

"hello".charAt(1); // "e"

A.3.5 indexOf

indexOf(searchString) - Returns first index of substring (-1 if not found)

"hello world".indexOf("world"); // 6
"hello world".indexOf("xyz");   // -1

A.3.6 slice

slice(start, end) - Extracts part of string

"hello world".slice(0, 5);  // "hello"
"hello world".slice(6);     // "world"

A.3.7 replace

replace(searchValue, replaceValue) - Replaces first occurrence

"hello world".replace("world", "there"); // "hello there"

A.3.8 replaceAll

replaceAll(searchValue, replaceValue) - Replaces all occurrences

"hello hello".replaceAll("hello", "hi"); // "hi hi"

A.3.9 split

split(separator) - Splits string into array

"red,blue,green".split(",");     // ["red", "blue", "green"]
"hello world".split(" ");        // ["hello", "world"]

A.3.10 trim

trim() - Removes whitespace from both ends

"  hello world  ".trim(); // "hello world"

A.3.11 includes

includes(searchString) - Tests if string contains substring

"hello world".includes("world"); // true
"hello world".includes("xyz");   // false

A.3.12 startsWith

startsWith(searchString) - Tests if string starts with substring

"hello world".startsWith("hello"); // true

A.3.13 endsWith

endsWith(searchString) - Tests if string ends with substring

"hello world".endsWith("world"); // true

A.4 Array Functions

A.4.1 length

length - Property that returns array length

[1, 2, 3].length; // 3

A.4.2 push

push(element) - Adds element to end, returns new length

let arr = [1, 2];
arr.push(3); // Returns 3, arr is now [1, 2, 3]

A.4.3 pop

pop() - Removes and returns last element

let arr = [1, 2, 3];
arr.pop(); // Returns 3, arr is now [1, 2]

A.4.4 unshift

unshift(element) - Adds element to beginning

let arr = [2, 3];
arr.unshift(1); // arr is now [1, 2, 3]

A.4.5 shift

shift() - Removes and returns first element

let arr = [1, 2, 3];
arr.shift(); // Returns 1, arr is now [2, 3]

A.4.6 indexOf

indexOf(searchElement) - Returns first index of element (-1 if not found)

[1, 2, 3, 2].indexOf(2); // 1
[1, 2, 3].indexOf(4);    // -1

A.4.7 includes

includes(searchElement) - Tests if array contains element

[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false

A.4.8 find

find(callback) - Returns first element that matches condition

let participants = [{id: 1, age: 22}, {id: 2, age: 19}];
participants.find(p => p.age > 20); // {id: 1, age: 22}

A.4.9 findIndex

findIndex(callback) - Returns index of first element that matches condition

[10, 20, 30].findIndex(x => x > 15); // 1

A.4.10 slice

slice(start, end) - Returns shallow copy of portion of array

[1, 2, 3, 4, 5].slice(1, 4); // [2, 3, 4]
[1, 2, 3, 4, 5].slice(2);    // [3, 4, 5]

A.4.11 splice

splice(start, deleteCount, ...items) - Changes array by removing/adding elements

let arr = [1, 2, 3, 4];
arr.splice(1, 2, 'a', 'b'); // Removes 2 elements at index 1, adds 'a', 'b'
// arr is now [1, 'a', 'b', 4]

A.4.12 concat

concat(array) - Combines arrays

[1, 2].concat([3, 4]); // [1, 2, 3, 4]

A.4.13 join

join(separator) - Joins array elements into string

[1, 2, 3].join(", "); // "1, 2, 3"
['red', 'blue'].join(" and "); // "red and blue"

A.4.14 reverse

reverse() - Reverses array in place

let arr = [1, 2, 3];
arr.reverse(); // arr is now [3, 2, 1]

A.4.15 sort

sort(compareFunction) - Sorts array in place

[3, 1, 2].sort(); // [1, 2, 3]
['banana', 'apple', 'cherry'].sort(); // ['apple', 'banana', 'cherry']

// Custom sort for numbers
[10, 2, 30].sort((a, b) => a - b); // [2, 10, 30]

A.4.16 forEach

forEach(callback) Executes function for each element

[1, 2, 3].forEach(x => console.log(x * 2)); // Prints 2, 4, 6

A.4.17 map

map(callback) - Creates new array with results of calling function on each element

[1, 2, 3].map(x => x * 2); // [2, 4, 6]

A.4.18 filter

filter(callback) - Creates new array with elements that pass test

[1, 2, 3, 4].filter(x => x > 2); // [3, 4]

A.4.19 reduce

reduce(callback, initialValue) - Reduces array to single value

[1, 2, 3, 4].reduce((sum, x) => sum + x, 0); // 10

A.4.20 some

some(callback) - Tests if at least one element passes test

[1, 2, 3].some(x => x > 2); // true

A.4.21 every

every(callback) - Tests if all elements pass test

[1, 2, 3].every(x => x > 0); // true
[1, 2, 3].every(x => x > 2); // false

A.5 Number Functions

A.5.1 parseInt

parseInt(string, radix) - Parses string and returns integer

parseInt("123");     // 123
parseInt("123.45");  // 123
parseInt("123abc");  // 123

A.5.2 parseFloat

parseFloat(string) - Parses string and returns floating point number

parseFloat("123.45");    // 123.45
parseFloat("123.45abc"); // 123.45

A.5.3 isNaN

isNaN(value) - Tests if value is NaN (Not a Number)

isNaN(123);     // false
isNaN("abc");   // true
isNaN("123");   // false

A.5.4 Number

Number(value) - Converts value to number

Number("123");    // 123
Number("123.45"); // 123.45
Number(true);     // 1
Number(false);    // 0

A.6 Object Functions

A.6.1 keys

Object.keys(obj) - Returns array of object’s property names

const obj = {a: 1, b: 2, c: 3};
Object.keys(obj); // ["a", "b", "c"]

A.6.2 values

Object.values(obj) - Returns array of object’s property values

const obj = {a: 1, b: 2, c: 3};
Object.values(obj); // [1, 2, 3]

A.6.3 entries

Object.entries(obj) - Returns array of [key, value] pairs

const obj = {a: 1, b: 2};
Object.entries(obj); // [["a", 1], ["b", 2]]