JavaScript Methods

***

Access the ultimate cheat sheet for JavaScript methods, updated to ES2024. Enhance your coding efficiency with this comprehensive guide ๐Ÿ› ๏ธ

This cheat sheet covers a wide range of JavaScript methods, including those added up to ES2024, with annotations indicating which version of ECMAScript introduced each method. Methods that were removed or are obsolete are excluded.

String Methods

let str = "Hello, World!";
 
// charAt()
console.log(str.charAt(0)); // "H"
 
// charCodeAt()
console.log(str.charCodeAt(0)); // 72
 
// codePointAt() (ES2015)
console.log(str.codePointAt(0)); // 72
 
// concat()
console.log(str.concat(" Welcome!")); // "Hello, World! Welcome!"
 
// endsWith() (ES2015)
console.log(str.endsWith("!")); // true
 
// includes() (ES2016)
console.log(str.includes("World")); // true
 
// indexOf()
console.log(str.indexOf("o")); // 4
 
// lastIndexOf()
console.log(str.lastIndexOf("o")); // 8
 
// localeCompare()
console.log(str.localeCompare("Hello")); // 1
 
// match()
console.log(str.match(/o/g)); // ["o", "o"]
 
// matchAll() (ES2020)
console.log(Array.from(str.matchAll(/o/g))); // [["o"], ["o"]]
 
// normalize() (ES2015)
let accented = "รฉ";
console.log(accented.normalize("NFD")); // "eฬ"
 
// padEnd() (ES2017)
console.log(str.padEnd(20, '.')); // "Hello, World!....."
 
// padStart() (ES2017)
console.log(str.padStart(20, '.')); // ".....Hello, World!"
 
// repeat() (ES2015)
console.log(str.repeat(2)); // "Hello, World!Hello, World!"
 
// replace()
console.log(str.replace("World", "Universe")); // "Hello, Universe!"
 
// replaceAll() (ES2021)
console.log(str.replaceAll("o", "0")); // "Hell0, W0rld!"
 
// search()
console.log(str.search("World")); // 7
 
// slice()
console.log(str.slice(7, 12)); // "World"
 
// split()
console.log(str.split(", ")); // ["Hello", "World!"]
 
// startsWith() (ES2015)
console.log(str.startsWith("Hello")); // true
 
// substring()
console.log(str.substring(7, 12)); // "World"
 
// toLowerCase()
console.log(str.toLowerCase()); // "hello, world!"
 
// toUpperCase()
console.log(str.toUpperCase()); // "HELLO, WORLD!"
 
// trim()
let paddedStr = "   Hello, World!   ";
console.log(paddedStr.trim()); // "Hello, World!"
 
// trimEnd() (ES2019)
console.log(paddedStr.trimEnd()); // "   Hello, World!"
 
// trimStart() (ES2019)
console.log(paddedStr.trimStart()); // "Hello, World!   "

Array Methods

let arr = [1, 2, 3, 4];
 
// at() (ES2022)
console.log(arr.at(-1)); // 4
 
// concat()
console.log(arr.concat([5, 6])); // [1, 2, 3, 4, 5, 6]
 
// copyWithin() (ES2015)
console.log(arr.copyWithin(1, 2)); // [1, 3, 4, 4]
 
// entries() (ES2015)
console.log(Array.from(arr.entries())); // [[0, 1], [1, 3], [2, 4], [3, 4]]
 
// every()
console.log(arr.every(num => num > 0)); // true
 
// fill() (ES2015)
console.log(arr.fill(0, 1, 3)); // [1, 0, 0, 4]
 
// filter()
console.log(arr.filter(num => num > 2)); // [4, 4]
 
// find() (ES2015)
console.log(arr.find(num => num > 2)); // 4
 
// findIndex() (ES2015)
console.log(arr.findIndex(num => num > 2)); // 2
 
// findLast() (ES2023)
console.log(arr.findLast(num => num > 2)); // 4
 
// findLastIndex() (ES2023)
console.log(arr.findLastIndex(num => num > 2)); // 3
 
// flat() (ES2019)
let nested = [1, [2, [3, [4]]]];
console.log(nested.flat(2)); // [1, 2, 3, [4]]
 
// flatMap() (ES2019)
console.log(arr.flatMap(x => [x * 2])); // [2, 0, 0, 8]
 
// forEach()
arr.forEach(num => console.log(num)); // 1 0 0 4
 
// includes() (ES2016)
console.log(arr.includes(2)); // false
 
// indexOf()
console.log(arr.indexOf(4)); // 3
 
// join()
console.log(arr.join("-")); // "1-0-0-4"
 
// keys() (ES2015)
console.log(Array.from(arr.keys())); // [0, 1, 2, 3]
 
// map()
console.log(arr.map(num => num * num)); // [1, 0, 0, 16]
 
// pop()
console.log(arr.pop()); // 4
 
// push()
console.log(arr.push(5)); // 4 (new length)
 
// reduce()
console.log(arr.reduce((total, num) => total + num)); // 1
 
// reduceRight()
console.log(arr.reduceRight((total, num) => total + num)); // 1
 
// reverse()
console.log(arr.reverse()); // [0, 0, 1]
 
// shift()
console.log(arr.shift()); // 0
 
// slice()
console.log(arr.slice(1, 3)); // [0, 1]
 
// some()
console.log(arr.some(num => num > 0)); // true
 
// sort()
console.log(arr.sort((a, b) => a - b)); // [0, 0, 1]
 
// splice()
console.log(arr.splice(1, 1, 5)); // [0] (removed element)
console.log(arr); // [0, 5, 1]
 
// toLocaleString()
console.log(arr.toLocaleString()); // "0,5,1"
 
// toString()
console.log(arr.toString()); // "0,5,1"
 
// unshift()
console.log(arr.unshift(-1)); // 4 (new length)
console.log(arr); // [-1, 0, 5, 1]
 
// values() (ES2015)
console.log(Array.from(arr.values())); // [-1, 0, 5, 1]

Object Methods

let target = { a: 1 };
let source = { b: 2, c: 3 };
 
// assign() (ES2015)
console.log(Object.assign(target, source)); // { a: 1, b: 2, c: 3 }
 
// create()
let proto = { greet: function() { return "Hello"; } };
let obj = Object.create(proto);
console.log(obj.greet()); // "Hello"
 
// defineProperties()
Object.defineProperties(target, {
  d: { value: 4, writable: true },
  e: { value: 5, writable: false }
});
console.log(target); // { a: 1, b: 2, c: 3, d: 4, e: 5 }
 
// defineProperty()
Object.defineProperty(target, "f", {
  value: 6,
  writable: false
});
console.log(target); // { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
 
// entries() (ES2017)
console.log(Object.entries(target)); // [["a", 1], ["b", 2], ["c", 3], ["d", 4], ["e", 5], ["f", 6]]
 
// freeze()
Object.freeze(target);
target.a = 42; // No effect
console.log(target.a); // 1
 
// fromEntries() (ES2019)
let entries = [["a", 1], ["b", 2]];
let objFromEntries = Object.fromEntries(entries);
console.log(objFromEntries); // { a: 1, b: 2 }
 
// getOwnPropertyDescriptor()
console.log(Object.getOwnPropertyDescriptor(target, "a")); // { value: 1, writable: true, enumerable: true, configurable: true }
 
// getOwnPropertyDescriptors() (ES2017)
console.log(Object.getOwnPropertyDescriptors(target));
 
// getOwnPropertyNames()
console.log(Object.getOwnPropertyNames(target)); // ["a", "b", "c", "d", "e", "f"]
 
// getOwnPropertySymbols()
let sym = Symbol("sym");
target[sym] = "symbolValue";
console.log(Object.getOwnPropertySymbols(target)); // [Symbol(sym)]
 
// getPrototypeOf()
console.log(Object.getPrototypeOf(target)); // {}
 
// hasOwn() (ES2022)
console.log(Object.hasOwn(target, "a")); // true
 
// is() (ES2015)
console.log(Object.is(25, 25)); // true
 
// isExtensible()
console.log(Object.isExtensible(target)); // false
 
// isFrozen()
console.log(Object.isFrozen(target)); // true
 
// isSealed()
console.log(Object.isSealed(target)); // true
 
// keys()
console.log(Object.keys(target)); // ["a",
 
 "b", "c", "d", "e", "f"]
 
// preventExtensions()
Object.preventExtensions(source);
console.log(Object.isExtensible(source)); // false
 
// seal()
Object.seal(target);
target.g = 7; // No effect
delete target.a; // No effect
console.log(target); // { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
 
// setPrototypeOf()
Object.setPrototypeOf(target, proto);
console.log(target.greet()); // "Hello"
 
// values() (ES2017)
console.log(Object.values(target)); // [1, 2, 3, 4, 5, 6]

Math Methods

// abs()
console.log(Math.abs(-4.5)); // 4.5
 
// acos()
console.log(Math.acos(1)); // 0
 
// acosh() (ES2015)
console.log(Math.acosh(1)); // 0
 
// asin()
console.log(Math.asin(0)); // 0
 
// asinh() (ES2015)
console.log(Math.asinh(0)); // 0
 
// atan()
console.log(Math.atan(0)); // 0
 
// atanh() (ES2015)
console.log(Math.atanh(0)); // 0
 
// atan2()
console.log(Math.atan2(0, 1)); // 0
 
// cbrt() (ES2015)
console.log(Math.cbrt(8)); // 2
 
// ceil()
console.log(Math.ceil(4.2)); // 5
 
// clz32() (ES2015)
console.log(Math.clz32(1)); // 31
 
// cos()
console.log(Math.cos(0)); // 1
 
// cosh() (ES2015)
console.log(Math.cosh(0)); // 1
 
// exp()
console.log(Math.exp(1)); // 2.718281828459045
 
// expm1() (ES2015)
console.log(Math.expm1(1)); // 1.718281828459045
 
// floor()
console.log(Math.floor(4.7)); // 4
 
// fround() (ES2015)
console.log(Math.fround(1.337)); // 1.3370000123977661
 
// hypot() (ES2015)
console.log(Math.hypot(3, 4)); // 5
 
// imul() (ES2015)
console.log(Math.imul(2, 3)); // 6
 
// log()
console.log(Math.log(10)); // 2.302585092994046
 
// log1p() (ES2015)
console.log(Math.log1p(0)); // 0
 
// log2()
console.log(Math.log2(8)); // 3
 
// log10()
console.log(Math.log10(100)); // 2
 
// max()
console.log(Math.max(1, 2, 3)); // 3
 
// min()
console.log(Math.min(1, 2, 3)); // 1
 
// pow()
console.log(Math.pow(2, 3)); // 8
 
// random()
console.log(Math.random()); // Random number between 0 and 1
 
// round()
console.log(Math.round(4.5)); // 5
 
// sign() (ES2015)
console.log(Math.sign(-5)); // -1
 
// sinh() (ES2015)
console.log(Math.sinh(0)); // 0
 
// sqrt()
console.log(Math.sqrt(16)); // 4
 
// tan()
console.log(Math.tan(0)); // 0
 
// tanh() (ES2015)
console.log(Math.tanh(0)); // 0
 
// trunc() (ES2015)
console.log(Math.trunc(4.9)); // 4

Date Methods

let date = new Date();
 
// getDate()
console.log(date.getDate()); // Current day of the month
 
// getDay()
console.log(date.getDay()); // Day of the week (0-6)
 
// getFullYear()
console.log(date.getFullYear()); // Current year
 
// getHours()
console.log(date.getHours()); // Current hour
 
// getMinutes()
console.log(date.getMinutes()); // Current minutes
 
// getMonth()
console.log(date.getMonth()); // Current month (0-11)
 
// getSeconds()
console.log(date.getSeconds()); // Current seconds
 
// getTime()
console.log(date.getTime()); // Milliseconds since January 1, 1970
 
// getTimezoneOffset()
console.log(date.getTimezoneOffset()); // Timezone offset in minutes
 
// now()
console.log(Date.now()); // Current time in milliseconds since January 1, 1970
 
// parse()
console.log(Date.parse("2022-05-27")); // Milliseconds since January 1, 1970 for the given date
 
// setDate()
date.setDate(15);
console.log(date.getDate()); // 15
 
// setFullYear()
date.setFullYear(2022);
console.log(date.getFullYear()); // 2022
 
// setHours()
date.setHours(20);
console.log(date.getHours()); // 20
 
// setMinutes()
date.setMinutes(45);
console.log(date.getMinutes()); // 45
 
// setMonth()
date.setMonth(11);
console.log(date.getMonth()); // 11 (December)
 
// setSeconds()
date.setSeconds(30);
console.log(date.getSeconds()); // 30
 
// toDateString()
console.log(date.toDateString()); // "Wed Dec 15 2022" (example)
 
// toISOString()
console.log(date.toISOString()); // "2022-12-15T20:45:30.000Z" (example)
 
// toLocaleDateString()
console.log(date.toLocaleDateString()); // "12/15/2022" (example, depending on locale)
 
// toLocaleTimeString()
console.log(date.toLocaleTimeString()); // "8:45:30 PM" (example, depending on locale)
 
// toTimeString()
console.log(date.toTimeString()); // "20:45:30 GMT+0000 (UTC)" (example)
 
// toUTCString()
console.log(date.toUTCString()); // "Wed, 15 Dec 2022 20:45:30 GMT" (example)
 
// valueOf()
console.log(date.valueOf()); // Milliseconds since January 1, 1970
 
// setUTCDate() (ES2017)
date.setUTCDate(15);
console.log(date.getUTCDate()); // 15
 
// setUTCFullYear() (ES2017)
date.setUTCFullYear(2022);
console.log(date.getUTCFullYear()); // 2022
 
// setUTCHours() (ES2017)
date.setUTCHours(20);
console.log(date.getUTCHours()); // 20
 
// setUTCMinutes() (ES2017)
date.setUTCMinutes(45);
console.log(date.getUTCMinutes()); // 45
 
// setUTCMonth() (ES2017)
date.setUTCMonth(11);
console.log(date.getUTCMonth()); // 11 (December)
 
// setUTCSeconds() (ES2017)
date.setUTCSeconds(30);
console.log(date.getUTCSeconds()); // 30

This cheat sheet includes methods from String, Array, Object, Math, and Date objects, annotated with the ECMAScript versions that introduced them. It excludes any obsolete or removed methods, providing a comprehensive yet concise reference for modern JavaScript development.