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

203
Views
Console log shows 'undefined' for a specific value of an array

I am trying to store the values that correspond to Sundays and Saturdays for the current month in an array (arr) and display it in the console, the variable 'd' represents the number of days in the current month.

When I try to display a specific value like console.log(arr[1]) it shows "undefined" in the console.

let arr = [];
console.log(arr);
console.log(arr[0]);

const renderDay = () => {
  let td = [];
  for (let i = 1; i <= d; i++) {
    if (
      days[i].toLocaleDateString("fr-FR", options).slice(0, 3) === "sam" ||
      days[i].toLocaleDateString("fr-FR", options).slice(0, 3) === "dim"
    ) {
      td.push(
        <td className="test" key={i}>
          {days[i].toLocaleDateString("fr-FR", options).slice(0, 3)}
        </td>
      );
      arr.push(i);
    } else
      td.push(
        <td key={i}>
          {days[i].toLocaleDateString("fr-FR", options).slice(0, 3)}
        </td>
      );
  }
  return td;
};

I wanted to check if the array is empty, so I display the whole array with console.log(arr),the output looks fine, it shows an array with length: 8 arr = {0: 6 , 1: 7 , 2: 13 , 3: 14 , 4: 20 , 5: 21 , 6: 27 , 7: 28}, who represents the index of Saturdays and Sundays in the current month, but I can't access to every specific value by index.

How I can access to every specific value by its index (arr[0],arr[1],arr[2]....)?

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

0

I think you are calling console.log too early. console.log(arr) only works, because a reference to that array is logged, which is updated as soon as renderDay is run. Check MDN here: console.log documentation

Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object, which is not necessarily the 'value' of the object at the moment in time you call console.log(), but it is the value of the object at the moment you open the console.

Try logging arr[0] after you called renderDay somewhere in your code.

You can also try to replace console.log(arr) with console.log(arr.toString()) to get the value arr has at the exact moment you log it - it should be empty when being called before renderDay is run.

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!