-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-data-types.js
More file actions
57 lines (43 loc) 路 1.83 KB
/
Copy path02-data-types.js
File metadata and controls
57 lines (43 loc) 路 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* MATERI: DATA TYPES (DEEP DIVE)
* Memahami bagaimana JavaScript menyimpan nilai di memori.
*/
// --- 1. PRIMITIVE TYPES (Immutable & Disimpan di Stack) ---
const nama = "Zidny"; // String
const umur = 25; // Number (Bisa bulat atau desimal)
const isCoding = true; // Boolean
const gap = null; // Null (Objek kosong yang disengaja)
let belumDiisi; // Undefined (Variabel dibuat tapi belum ada nilai)
// Fitur Baru ES6+: Symbol (Unik & Tidak Tergantikan)
const id1 = Symbol("id");
const id2 = Symbol("id");
console.log("Apakah Symbol sama?", id1 === id2); // false (Selalu Unik)
// Fitur Baru ES2020: BigInt (Untuk angka yang sangat besar)
const angkaGede = 9007199254740991n;
console.log(typeof angkaGede); // "bigint"
// --- 2. REFERENCE TYPES (Mutable & Disimpan di Heap) ---
// Object, Array, dan Function sebenarnya adalah 'Object'
const user = { name: "Alex" };
const hobi = ["Music", "Sport"];
console.log(typeof hobi); // "object" (Array adalah bentuk khusus dari Object)
// --- 3. PERBEDAAN KRUSIAL: VALUE vs REFERENCE ---
// Primitive: Copy by Value
let a = 10;
let b = a; // Nilai 10 di-copy ke b
b = 20;
console.log(a); // Tetap 10
// Reference: Copy by Reference (Berbagi alamat memori)
let person1 = { name: "Budi" };
let person2 = person1; // person2 menunjuk ke alamat memori yang sama
person2.name = "Siti";
console.log(person1.name); // Ikut berubah jadi "Siti"!
// (Inilah alasan mengapa kita butuh Spread Operator [...] untuk copy data)
// --- 4. TYPE COERCION (Keunikan JavaScript) ---
console.log("5" + 2); // "52" (Angka diubah jadi string)
console.log("5" - 2); // 3 (String diubah jadi angka)
console.log(false == 0); // true (Loose equality)
console.log(false === 0); // false (Strict equality - SELALU GUNAKAN INI)
/**
* TIPS: Selalu gunakan === daripada == untuk menghindari
* bug akibat konversi tipe data otomatis.
*/