Skip to content

Commit da292ea

Browse files
feat: add modern js
1 parent 429744a commit da292ea

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// INFO: ES6 Primitives & Meta Types
2+
3+
/*
4+
1. Symbols
5+
Unique and immutable values, ideal for object keys that shouldn't collide.
6+
*/
7+
const id = Symbol("userID");
8+
obj[id] = 123;
9+
10+
/*
11+
2. BigInt
12+
For integer values beyond Number.MAX_SAFE_INTEGER
13+
*/
14+
const big = 9007199254740991n + 1n;
15+
console.log(big); // 9007199254740992n
16+
17+
/*
18+
3. Sets
19+
Stores unique values.
20+
Good for deduplication
21+
*/
22+
const s = new Set([1, 2, 2, 4]);
23+
console.log([...s]);
24+
25+
/*
26+
4. Map
27+
key-value store with any type as key (including objects)
28+
*/
29+
const m = new Map();
30+
m.set({ id: 1 }, 'data');
31+
32+
/*
33+
5. Proxy & Reflect
34+
proxy lets you intercept operations like get/set
35+
*/
36+
const p = new Proxy({}, {
37+
get: (obj, prop) => prop in obj ? obj[prop] : 42
38+
});
39+
console.log(p.any); // 42
40+
41+
/*
42+
6. Destructuring
43+
Simplifies extracting values
44+
*/
45+
const { a, b } = obj;
46+
const [x, y] = arr;
47+
48+
/*
49+
7. Optional Chaning(?.)
50+
Safely access nested properties or methods
51+
*/
52+
const user = {
53+
name: "Alice",
54+
address: {
55+
city: "New York",
56+
zip: 10001
57+
}
58+
};
59+
console.log(user.address?.city); // "New York"
60+
console.log(user.contact?.phone); // undefined (no error)
61+
62+
/*
63+
8. Nullish (??)
64+
Default only if value is null or undefined
65+
*/
66+
const n = val ?? "defalut";

0 commit comments

Comments
 (0)