Javascript Cheatsheet

JS
R
JavaScript

Updated to ECMAScript 2022

1// ##########################################
2// Nullish Coalescing
3const foo = null ?? 'default string';
4console.log(foo);
5
6// ##########################################
7// Private fields
8class ClassWithPrivateField { 
9  #privateField 
10}
11
12// ##########################################
13// Logical Assignment
14const a = { duration: 50, title: '' };
15a.duration ||= 10;
16console.log(a.duration); // expected output: 50
17
18a.title ||= 'title is empty.';
19console.log(a.title);
20// expected output: "title is empty"
21
22// ##########################################
23// Random
24Promise.any()
25Array.prototype.at()
26Temporal
27String.prototype.replaceAll()
28Numeric Separators
29Promise.allSettled()
30Proxies
31
32// ##########################################
33// Array.findLast()
34Array.findLast()
35const array1 = [5, 12, 50, 130, 44];
36const found = array1.findLast((element) => element > 45);
37console.log(found);  // expected output: 130
38
39// ##########################################
40// Error.prototype.cause
41try {
42  connectToDatabase();
43} catch (err) {
44  throw new Error('Connecting to database failed.', { cause: err });
45}
46
47// ##########################################
48// Object.hasOwn()
49const object1 = {
50  prop: 'exists'
51};
52console.log(Object.hasOwn(object1, 'prop')); // expected output: true
53
54// ##########################################
55// Regexp Match Indices
56const str1 = "foo bar foo";
57const regex1 = /foo/dg;
58console.log(regex1.exec(str1).indices[0]); // Output: Array [0, 3]
59
60
61
62

Created on 11/25/2022