Kiểm tra "rỗng" có nhiều nghĩa: null, undefined, chuỗi rỗng "", chuỗi chỉ khoảng trắng " ", mảng rỗng [], object rỗng {}, số 0, NaN, false. Cách kiểm tra tùy yêu cầu nghiệp vụ.
1. Kiểm tra null hoặc undefined (Nullish)
// ✅ Cách ngắn gọn (loose equality - chấp nhận được)
if (value == null) { /* null hoặc undefined */ }
// ✅ Cách rõ ràng (strict)
if (value === null || value === undefined) { /* null hoặc undefined */ }
// ✅ Modern: Nullish coalescing (default value)
const result = value ?? "default"; // Chỉ null/undefined mới dùng default
2. Kiểm tra "Falsy" (Bao gồm 0, "", false, NaN, null, undefined)
if (!value) { /* falsy */ }
// Tương đương:
if (value === false || value === 0 || value === "" || value === null || value === undefined || Number.isNaN(value))
⚠️ Cẩn thận: 0, "", false là giá trị HỢP LỆ trong nhiều trường hợp (số lượng 0, chuỗi rỗng ý nghĩa, flag false).
3. Kiểm tra chuỗi rỗng / chỉ khoảng trắng (Blank string)
// Chỉ chuỗi rỗng
if (str === "") { ... }
// Chuỗi rỗng HOẶC chỉ whitespace
if (!str || str.trim() === "") { ... }
// Hoặc
if (str == null || str.trim() === "") { ... }
// Helper function
const isBlank = (str) => str == null || str.trim() === "";
isBlank(""); // true
isBlank(" "); // true
isBlank(" hello "); // false
isBlank(null); // true
isBlank(undefined); // true
4. Kiểm tra mảng/object rỗng
// Array rỗng
if (Array.isArray(arr) && arr.length === 0) { ... }
// Object rỗng (không có own property)
if (Object.keys(obj).length === 0) { ... }
// Hoặc ES2017+
if (Object.entries(obj).length === 0) { ... }
5. Hàm utility tổng hợp
function isEmpty(value) {
if (value == null) return true; // null, undefined
if (typeof value === "string") return value.trim() === "";
if (Array.isArray(value)) return value.length === 0;
if (typeof value === "object") return Object.keys(value).length === 0;
return false; // number, boolean, function, symbol, bigint không coi là "empty"
}
// Test
isEmpty(null); // true
isEmpty(undefined); // true
isEmpty(""); // true
isEmpty(" "); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty(0); // false
isEmpty(false); // false
isEmpty("hello"); // false
isEmpty([1,2]); // false
isEmpty({a:1}); // false
6. Kiểm tra biến có tồn tại (khai báo) – tránh ReferenceError
// ✅ An toàn cho biến có thể chưa khai báo
if (typeof myVar !== "undefined") { ... }
// ❌ Nguy hiểm: ReferenceError nếu myVar chưa khai báo
if (myVar != null) { ... }
Ví dụ từ bài gốc (đã mở rộng)
<script>
var age = 10;
// Truthy check
if (age) {
document.write("True - age truthy");
}
// typeof check (an toàn cho undeclared)
if (typeof age !== 'undefined') {
document.write("
True - age đã khai báo");
}
// Nullish check
var maybeNull = null;
if (maybeNull == null) {
document.write("
maybeNull là null hoặc undefined");
}
</script>
Tóm tắt: Chọn cách nào?
| Yêu cầu | Cách kiểm tra |
|---|---|
| Chỉ null/undefined | value == null hoặc value === null \|\| value === undefined |
| Tất cả falsy (0, "", false, NaN, null, undefined) | !value |
| Chuỗi rỗng hoặc whitespace | str == null \|\| str.trim() === "" |
| Mảng rỗng | Array.isArray(arr) \&\& arr.length === 0 |
| Object rỗng | Object.keys(obj).length === 0 |
| Biến có thể chưa khai báo | typeof x !== "undefined" |
| Default value cho null/undefined | value ?? defaultValue |