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

139
Views
Uncaught TypeError: Cannot read properties of undefined even when using typeof- better approach?

My loop checks if an array of cards has a special attribute, if so it does a certain action.

  if(typeof cardArray[i]['special']!=='undefined'){
    // do stuff
}

Unfortunately, even though I'm using typeof to check the array contains a defined special key I'm still getting

Uncaught TypeError: Cannot read properties of undefined (reading 'special')

What am I doing wrong here or is there a better approach for this in general?

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

0

As mentioned in the comments, your problem is probably inside your loop.
Take a look at this example running without errors:

const cardArray = [{
  'special': 'pasta'
  }, {
  'regular': 'bread'
}]

for (let i = 0; i < cardArray.length; i++) {
  console.log(`Loop number ${i}`)
  if(typeof cardArray[i]['special']!=='undefined'){
    console.log(`Found: ${cardArray[i]['special']}`)
  }
}

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!