Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
map[complement] === undefined vs !map[complement]

I am solving the following leetcode question and have the solution below

const twoSum = (numbers, target) => {
    let map = {}
    let result = []
    for (let i = 0; i < numbers.length; i++) {
        let complement = target - numbers[i]
        if (map[complement] === undefined) {
            map[numbers[i]] = i
        } else {
            result[0] = map[complement] + 1
            result[1] = i + 1
        }
    }
    return result
};

If I replace map[complement] === undefined with !map[complement] I return an empty array. In my mind both should return true. Why does the latter breaks my code?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

map[complement] === undefined only becomes true when there is no element with the key equal to complement while !map[complement] becomes true in all the cases where its result is a falsy value.

Falsy values include but are not limited to, "", false, undefined, null, 0, -0, etc.

In other words, the first case is a sub set of the second.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!