skip to Main Content

Following Colt Steele WebDev Bootcamp 2023, following his For…Of Loop lesson in Javascript, i have typed out what i think is exactly what he has (not copied and pasted), I get an error Uncaught TypeError: Cannot read properties of undefined (reading ‘Jane’)

const seatingChart = [
    ['Kristen', 'Erik', 'Namita']
    ['Geoffry', 'Juanita', 'Antonio', 'Kevin']
    ['Yuma', 'Sakura', 'Jack', 'Jane']
]

for (let row of seatingChart) {
    for (let student of row) {
        console.log(student);
    }
}

What is expected is a list of students in the browser console.
What i get is "Uncaught TypeError: Cannot read properties of undefined (reading ‘Jane’)"

Is this an out of date method in Javascript?

I have looked at Mozilla Docs For…Of loops section and cant see how this has created an error as in the lesson Colt Steeles output is what is expected.

2

Answers


  1. You have a syntax error in your seatingChart array.
    You are missing commas to separate the subarrays
    This is updated code:

    const seatingChart = [
        ['Kristen', 'Erik', 'Namita'],
        ['Geoffry', 'Juanita', 'Antonio', 'Kevin'],
        ['Yuma', 'Sakura', 'Jack', 'Jane']
    ];
    
    for (let row of seatingChart) {
        for (let student of row) {
            console.log(student);
        }
    }

    Note the added commas after each subarray. In JavaScript, you need to separate elements in an array with commas.

    Login or Signup to reply.
  2. I totally get the frustration and the error does not describe really what you are facing… It’s actually a simple error caused by missing 2 "," (commas) in your code. You have arrays nested in an array, but you don’t comma separate those arrays, and so JavaScript gets confused what you are trying to do.

    const seatingChart = [
        ['Kristen', 'Erik', 'Namita'],
        ['Geoffry', 'Juanita', 'Antonio', 'Kevin'],
        ['Yuma', 'Sakura', 'Jack', 'Jane']
    ]
    

    Notice the two , at dividing the three separate arrays.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search