Comprehensive reference with definitions and examples for all built-in JavaScript methods
Creates a new array by applying a function to each element
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(n => n * 2);
// Result: [2, 4, 6, 8]Creates a new array with elements that pass a test
const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(n => n % 2 === 0);
// Result: [2, 4]Reduces array to single value by applying function
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, n) => acc + n, 0);
// Result: 10Executes a function for each array element
const fruits = ['apple', 'banana'];
fruits.forEach(fruit => console.log(fruit));Returns first element that satisfies condition
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
const user = users.find(u => u.id === 2);
// Result: {id: 2, name: 'Bob'}Returns index of first element that satisfies condition
const numbers = [5, 12, 8, 130, 44];
const index = numbers.findIndex(n => n > 10);
// Result: 1Tests if at least one element passes condition
const numbers = [1, 2, 3, 4];
const hasEven = numbers.some(n => n % 2 === 0);
// Result: trueTests if all elements pass condition
const numbers = [2, 4, 6];
const allEven = numbers.every(n => n % 2 === 0);
// Result: trueChecks if array contains a value
const fruits = ['apple', 'banana', 'mango'];
const hasBanana = fruits.includes('banana');
// Result: trueFlattens nested arrays by specified depth
const nested = [1, [2, [3, 4]]];
const flattened = nested.flat(2);
// Result: [1, 2, 3, 4]Maps then flattens the result by one level
const arr = [1, 2, 3];
const result = arr.flatMap(x => [x, x * 2]);
// Result: [1, 2, 2, 4, 3, 6]Returns shallow copy of array portion
const fruits = ['apple', 'banana', 'mango', 'orange'];
const citrus = fruits.slice(2, 4);
// Result: ['mango', 'orange']Changes array by removing/replacing/adding elements
const fruits = ['apple', 'banana', 'mango'];
fruits.splice(1, 1, 'orange');
// Result: ['apple', 'orange', 'mango']push: adds to end, pop: removes from end
const stack = [1, 2];
stack.push(3); // [1, 2, 3]
stack.pop(); // [1, 2]shift: removes from start, unshift: adds to start
const arr = [2, 3];
arr.unshift(1); // [1, 2, 3]
arr.shift(); // [2, 3]Merges two or more arrays
const arr1 = [1, 2];
const arr2 = [3, 4];
const merged = arr1.concat(arr2);
// Result: [1, 2, 3, 4]Joins array elements into a string
const words = ['Hello', 'World'];
const sentence = words.join(' ');
// Result: 'Hello World'Reverses array in place
const arr = [1, 2, 3];
arr.reverse();
// Result: [3, 2, 1]Sorts array in place
const numbers = [3, 1, 4, 1, 5];
numbers.sort((a, b) => a - b);
// Result: [1, 1, 3, 4, 5]Fills array elements with static value
const arr = [1, 2, 3, 4];
arr.fill(0, 1, 3);
// Result: [1, 0, 0, 4]Returns first index of value, or -1
const arr = ['a', 'b', 'c', 'b'];
const index = arr.indexOf('b');
// Result: 1Returns last index of value, or -1
const arr = ['a', 'b', 'c', 'b'];
const index = arr.lastIndexOf('b');
// Result: 3Returns element at index (supports negative)
const arr = [1, 2, 3, 4];
const last = arr.at(-1);
// Result: 4Creates array from array-like or iterable
const str = 'hello';
const chars = Array.from(str);
// Result: ['h', 'e', 'l', 'l', 'o']Checks if value is an array
Array.isArray([1, 2, 3]); // true
Array.isArray('hello'); // falseCreates array from arguments
const arr = Array.of(1, 2, 3);
// Result: [1, 2, 3]Extracts part of string
const str = 'Hello World';
const part = str.slice(0, 5);
// Result: 'Hello'Extracts characters between two indices
const str = 'Hello World';
const part = str.substring(6, 11);
// Result: 'World'Splits string into array
const str = 'a,b,c';
const arr = str.split(',');
// Result: ['a', 'b', 'c']Removes whitespace from string
const str = ' hello ';
str.trim(); // 'hello'
str.trimStart(); // 'hello '
str.trimEnd(); // ' hello'Replaces substring(s)
const str = 'hello world';
str.replace('world', 'there'); // 'hello there'
'aaa'.replaceAll('a', 'b'); // 'bbb'Checks if string contains substring
const str = 'Hello World';
str.includes('World'); // trueChecks if string starts/ends with substring
const str = 'Hello World';
str.startsWith('Hello'); // true
str.endsWith('World'); // trueReturns index of substring or -1
const str = 'hello world';
str.indexOf('o'); // 4
str.lastIndexOf('o'); // 7Converts string case
const str = 'Hello';
str.toUpperCase(); // 'HELLO'
str.toLowerCase(); // 'hello'Pads string to target length
const str = '5';
str.padStart(3, '0'); // '005'
str.padEnd(3, '0'); // '500'Repeats string n times
const str = 'ha';
str.repeat(3); // 'hahaha'Gets character or character code at index
const str = 'Hello';
str.charAt(0); // 'H'
str.charCodeAt(0); // 72Matches string against regex
const str = 'test1 test2';
str.match(/\d/g); // ['1', '2']Searches for regex match, returns index
const str = 'hello world';
str.search(/world/); // 6Returns character at index (supports negative)
const str = 'hello';
str.at(-1); // 'o'String interpolation with backticks
const name = 'Alice';
const greeting = `Hello, ${name}!`;
// Result: 'Hello, Alice!'Returns array of object's keys
const obj = {a: 1, b: 2};
Object.keys(obj);
// Result: ['a', 'b']Returns array of object's values
const obj = {a: 1, b: 2};
Object.values(obj);
// Result: [1, 2]Returns array of [key, value] pairs
const obj = {a: 1, b: 2};
Object.entries(obj);
// Result: [['a', 1], ['b', 2]]Copies properties from source to target
const target = {a: 1};
const source = {b: 2};
Object.assign(target, source);
// Result: {a: 1, b: 2}Freezes object (immutable)
const obj = {a: 1};
Object.freeze(obj);
obj.a = 2; // Fails silently
// obj is still {a: 1}Seals object (can't add/remove properties)
const obj = {a: 1};
Object.seal(obj);
obj.a = 2; // Works
obj.b = 3; // FailsCreates object with specified prototype
const proto = {greet() { return 'Hi'; }};
const obj = Object.create(proto);
obj.greet(); // 'Hi'Checks if object has own property
const obj = {a: 1};
Object.hasOwn(obj, 'a'); // true
Object.hasOwn(obj, 'toString'); // falseCreates object from [key, value] pairs
const entries = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(entries);
// Result: {a: 1, b: 2}Compares two values for equality
Object.is(NaN, NaN); // true
Object.is(0, -0); // false
Object.is({}, {}); // falseFormats number to fixed decimal places
const num = 3.14159;
num.toFixed(2); // '3.14'Converts number to string
const num = 255;
num.toString(); // '255'
num.toString(16); // 'ff' (hex)Parses string to number
parseInt('42'); // 42
parseFloat('3.14'); // 3.14
parseInt('FF', 16); // 255Checks if value is NaN
Number.isNaN(NaN); // true
Number.isNaN('hello'); // falseChecks if value is finite number
Number.isFinite(42); // true
Number.isFinite(Infinity); // falseChecks if value is integer
Number.isInteger(42); // true
Number.isInteger(3.14); // falseReturns maximum/minimum value
Math.max(1, 5, 3); // 5
Math.min(1, 5, 3); // 1
Math.max(...[1,2,3]); // 3Rounds number down/up/nearest
Math.floor(3.7); // 3
Math.ceil(3.2); // 4
Math.round(3.5); // 4Returns absolute value
Math.abs(-5); // 5
Math.abs(3); // 3Square root and power
Math.sqrt(16); // 4
Math.pow(2, 3); // 8
2 ** 3; // 8 (same)Returns random number [0, 1)
Math.random(); // 0.547...
Math.floor(Math.random() * 10); // 0-9Removes decimal part
Math.trunc(3.7); // 3
Math.trunc(-3.7); // -3Waits for all promises to resolve
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
Promise.all([p1, p2]).then(console.log);
// [1, 2]Waits for all promises to settle
const p1 = Promise.resolve(1);
const p2 = Promise.reject('error');
Promise.allSettled([p1, p2]);
// [{status:'fulfilled',value:1}, {status:'rejected',reason:'error'}]Returns first settled promise
const slow = new Promise(r => setTimeout(() => r('slow'), 100));
const fast = Promise.resolve('fast');
Promise.race([slow, fast]); // 'fast'Returns first fulfilled promise
const p1 = Promise.reject('err');
const p2 = Promise.resolve('ok');
Promise.any([p1, p2]); // 'ok'Handles promise results
fetch('/api/data')
.then(res => res.json())
.catch(err => console.error(err))
.finally(() => console.log('Done'));Converts JavaScript value to JSON string
const obj = {name: 'Alice', age: 30};
JSON.stringify(obj);
// '{"name":"Alice","age":30}'Parses JSON string to JavaScript value
const json = '{"name":"Alice"}';
const obj = JSON.parse(json);
// {name: 'Alice'}Creates date object
const now = new Date();
const specific = new Date('2024-01-01');Returns current timestamp (ms)
const timestamp = Date.now();
// 1704067200000Gets date components
const date = new Date('2024-03-15');
date.getFullYear(); // 2024
date.getMonth(); // 2 (0-indexed)
date.getDate(); // 15Converts date to ISO string
const date = new Date('2024-01-01');
date.toISOString();
// '2024-01-01T00:00:00.000Z'Key-value pairs with any key type
const map = new Map();
map.set('key', 'value');
map.get('key'); // 'value'
map.has('key'); // true
map.delete('key');
map.size; // 0Collection of unique values
const set = new Set([1, 2, 2, 3]);
set.size; // 3
set.has(2); // true
set.add(4);
set.delete(1);
[...set]; // [2, 3, 4]Logs messages to console
console.log('Info');
console.warn('Warning');
console.error('Error');Displays data as table
const users = [{name:'Alice',age:30}, {name:'Bob',age:25}];
console.table(users);Measures execution time
console.time('loop');
for(let i = 0; i < 1000; i++) {}
console.timeEnd('loop');