Useful JS Cheat Sheet

Useful JS Cheat Sheet

1. Array Methods 1:

[1, 2, 3].length; // 3
[1, 2, 3].push(4); // [1, 2, 3, 4]
[1, 2, 3].unshift(0); // [0, 1, 2, 3]
[1, 2, 3].pop(); // [1, 2]
[1, 2, 3].shift(); // [2, 3]
[1, 2, 3].at(2); // 3
[1, 2, 3].indexOf(3); // 2
[1, 2, 3].includes(3); // true
[1, 2, 3].map(num) => num * 2); // [2, 4, 6]
[1, 2, 3].filter((num) => num > 1); // [2, 3]
[1, 2, 3].every((num) => num > 0); // true
[1, 2, 3].some((num) => num >2); // true
[1, 2, 3].fill(0); // [0, 0, 0]
[1, 2, 3].reduce((acc, num) => acc + num, 0); //6
[1, 2, 3].concat([4, 5]); // [1, 2, 3, 4, 5]
[1, 2, 3]. reverse(); // [3, 2, 1]
[3, 1, 2].sort(); // [1, 2, 3]
[1, 2, 3].join("-"); // "1-2-3"
[1, 2, 3].flat(); // [1, 2, 3]
[1, 2, 3].find((num) => num === 1); //1
[1, 2, 3].findIndex((num) => num === 2); //1
[1, 2, 3].toString(); // "1,2,3"
[1, 2, 3].toLocaleString(); // "1,2,3"
[1, 2, 3].slice(1, 2); // [2]
[1, 2, 3].splice(1, 2, "a"); // [1, 'a', 3]
Array.isArray([1, 2, 3]); // true
Array.from("123"); // ['1', '2', '3']

2. Array Methods 2:

"JavaScript".length; // 10
"JavaScript"[2] // 'v'
"JavaScript".charAt(2); // 'v'
"JavaScript".charCodeAt(2); // 118
"JavaScript".indexOf("S") // 4
"JavaScript".toLowerCase(); // 'javascript'
"JavaScript".toUpperCase(); // 'JAVASCRIPT'
"JavaScript".slice(2, 5); // 'vaS'
"JavaScript".substring(2, 5); // 'vaS'
"JavaScript".substr(2, 2); // 'va'
"JavaScript".concat(" Dev"); // 'JavaScript Dev'
"JavaScript Dev".split(" "); // ['JavaScript', 'Dev']
"JavaScript Dev".includes("Dev"); // true
"Java Dev Developer".replace("Dev", "JS"); // 'Java JS Developer'
"Java Dev Developer".replaceAll("Dev", "JS"); // 'Java JS JSeloper'
" JavaScript Dev  ".trim(); // 'JavaScript Dev'
" JavaScript Dev  ".trimStart(); // 'JavaScript Dev  '
" JavaScript Dev  ".trimEnd(); // ' JavaScript Dev'
"Dev".padStart(10, "*"); // '*******Dev'
"Dev".padEnd(10, "*"); // 'Dev*******'
"JavaScript Dev".startsWith("Java"); // true
"JavaScript Dev".endsWith("Dev"); // true
"JavaScript Dev".repeat(3); // 'JavaScript DevJavaScript DevJavaScript Dev'
"JavaScript Dev Developer".indexOf("Dev"); //11
"JavaScript Dev Developer".lastIndexOf("Dev"); // 15
"JavaScript Dev Developer".search("Dev"); // 11
"JavaScript Dev".includes("Dev"); // true

3. Date Methods:

Initialization

const date = new Date();
// 2024-12-06T19:27:47.447Z
new Date(1685478925456);
// 2023-05-30T20:35:25.456Z
new Date("2024-12-06");
// 2024-12-06T00:00:00.000Z
new Date("2024-12-06T04:12:00");
// 2024-12-06T04:12:00.000Z	
new Date(2024, 12, 5, 1, 10, 0, 1);
// year, month, day, hour, min, sec, misc
//2025-01-05T01:10:00.001Z

Conversions

// Initial Date:
const date = new Date(2024, 12, 5, 1, 10, 0, 1);
// Conversions:
date.toString(); 
// 'Sun Jan 05 2025 01:10:00 GMT+0000 (Coordinated Universal Time)'
date.toDateString();
// 'Sun Jan 05 2025'
date.toTimeString();
// '01:10:00 GMT+0000 (Coordinated Universal Time)'
date.toISOString();
// '2025-01-05T01:10:00.001Z'
date.toLocaleString();
// '1/5/2025, 1:10:00 AM'
date.toLocaleDateString();
// '1/5/2025'
date.toLocaleTimeString();
// '1:10:00 AM'

Get Methods

// Initial Date:
const date = new Date(2024, 10, 5, 1, 10, 0, 99);
// Get Methods:
date.getFullYear(); // 2024
date.getMonth(); // 10
date.getDate(); // 5
date.getDay(); // 2
date.getHours(); // 1
date.getMinutes(); // 10
date.getSeconds(); // 0
date.getMilliseconds(); // 99
date.getTime(); // '1736039400001'
date.getTimezoneOffset(); // 0

Set Methods

date.setFullYear(2024); // set Year
date.setMonth(11); // set Month
date.setDate(31);  // set Date
date.setHours(23); // set Hours
date.setMinutes(22); // set Minutes
date.setSeconds(55); // set Seconds
date.setMilliseconds(88); // set Milliseconds
date.setTime(1685478925456); // set Time (milliseconds since Jan 1, 1970)

Do you enjoy this blog post?

Read more