JS **không có cách explicit allocate memory** (khác C/C++ malloc). Bộ nhớ được cấp phát **ngầm (implicit)** khi bạn khởi tạo giá trị/object.
Ví dụ
function test() {
let x = { name: "John", age: 24 }; // JS tự động allocate memory cho object
console.log(x);
return null; // x out of scope → GC tự động free
}
test();
Các kiểu allocate ngầm
- Primitive:
let n = 42,let s = "hello" - Object:
let o = {},let arr = [] - Function:
function f() {}(function cũng là object) - Closure: capture biến outer → allocate environment record
Kết luận: Chỉ việc code bình thường — JS lo phần allocate/free. Tập trung tránh memory leak (unwanted references) thay vì lo allocate.