Logical assignment kết hợp phép logic với phép gán. &&= gán chỉ khi bên trái truthy: user.name &&= user.name.trim() — chỉ trim nếu name có giá trị. ||= gán khi bên trái falsy: config.timeout ||= 3000 để đặt default. ??= gán chỉ khi null/undefined (an toàn hơn ||= vì không ghi đè 0 hay ''): options.retries ??= 3.
Sự khác biệt quan trọng với ||= và ??=: nếu giá trị là 0 hoặc chuỗi rỗng, ||= sẽ ghi đè còn ??= thì không.
Logical assignment combines logical operations with assignment. &&= assigns only when the left side is truthy: user.name &&= user.name.trim() — only trims if name has a value. ||= assigns when the left side is falsy: config.timeout ||= 3000 to set a default. ??= assigns only when null/undefined (safer than ||= since it will not overwrite 0 or ''): options.retries ??= 3.
Key difference between ||= and ??=: if the value is 0 or empty string, ||= will overwrite it but ??= will not.