Introduction
We have all written that useful function called twoSum: loop through an array, find two numbers that add up to a target, and return their indices. It seems simple, but in JavaScript a value as innocent as 0 can become a subtle trap.
The basic idea many of us use is to maintain a map of values to indices and, for each element, calculate the complement with target minus the current value. If the complement is in the map, we return the indices. It appears to work in simple examples like [2, 7, 11, 15] with target 9, which returns [0, 1].
The problem arises when the index stored in the map is 0. If existence is checked with a truthy check like if (map[complement]), the value 0 is considered falsy and the condition fails. For example, with the array [3, 2, 4] and target 6, the correct solution is [1, 2], but the code may return an empty array because map[3] is 0 and the check fails.
Step-by-step explanation: we store indices in map; when complement is 3, we store map[3] = 0; the condition if (map[complement]) evaluates to false if map[complement] is 0; therefore, a valid match is ignored.
The solution is not to rely on truthy checks that fail with 0 or other falsy values. Instead, you can use hasOwnProperty to check if the key exists: if (map.hasOwnProperty(complement)). Or safe alternatives are using key in obj or using typeof obj[key] !== undefined, comparing with the string undefined in the language's practice. It is also possible to use Object.prototype.hasOwnProperty.call(map, complement) to avoid collisions with inherited properties.
With this change, the algorithm works correctly again and returns [1, 2] in the previous case. The moral is that in JavaScript, 0, null, undefined, false, empty string, and NaN are falsy values, so if (obj[key]) can break the logic if the legitimate value is 0 or false.
Real case: I ran into this bug in a small front-end app where I mapped component states to form field identifiers. The field with id 0 was skipped in validation, and the failure took a while to appear until I reviewed the key existence check.
Quick best practices: avoid if(obj[key]) if key can map to 0 or false; use obj.hasOwnProperty(key) or key in obj; for more explicit comparisons, use typeof obj[key] to detect undefined. Understanding JavaScript truthiness helps avoid surprises.
Q2BSTUDIO and how we can help
At Q2BSTUDIO, we are a company specialized in custom software and application development. We offer custom software services and custom application solutions aligned with business needs. We are experts in artificial intelligence and can integrate AI for companies through custom AI agents and machine learning solutions that improve processes and user experiences.
We also provide cybersecurity services to protect applications and data, and we have AWS and Azure cloud services to deploy scalable and secure infrastructures. Our business intelligence services include Power BI implementations and data pipelines to transform information into actionable decisions.
If you need to develop custom software, integrate artificial intelligence into your processes, deploy to the cloud with AWS and Azure cloud services, or improve security with professional cybersecurity, at Q2BSTUDIO we can help you with turnkey solutions and technical consulting.
Quick TLDR summary
Avoid if(obj[key]) when key can return 0, false, or other falsy values. Use hasOwnProperty, key in obj, or typeof checks to detect key existence. JavaScript truthiness is useful but can cause hard-to-detect bugs.
Do you want us to review your code or design a custom solution with artificial intelligence, AI agents, Power BI, and AWS and Azure cloud services? Contact Q2BSTUDIO for personalized consulting in development, custom software, cybersecurity, and business intelligence services.


