1

Im testing my ability to create multi dimensional arrays with for loops with:

        for (var a = 0; a < 3; a++){
            var pax[a] = new Array();
            pax[a].push(1,2,3);
        }

        console.log(pax[2][1],pax[0][0]);

But its giving me a syntax error unexpected token at the [ of var pax[a]

I think it actually might be a scope error vs a syntax error, but I cant be sure.

3
  • 2
    var pax[a] is invalid syntax. Isn't the console telling you that? Commented Jun 19, 2014 at 3:10
  • pax is a variable, pax[a] is an expression that refers to an element from the array or object that's the value of the pax variable. Commented Jun 19, 2014 at 3:11
  • as a side note, the preferred JavaScript syntax for arrays is var pax = []; Commented Jun 19, 2014 at 3:28

1 Answer 1

2

syntax issue as @Barmar pointed out.

try...

var pax = new Array();
 for (var a = 0; a < 3; a++){
    pax[a] = new Array();
    pax[a].push(1,2,3);
}

console.log(pax[2][1],pax[0][0]);
Sign up to request clarification or add additional context in comments.

1 Comment

The updated code is correct, but it's not a scope issue. JS has function scope, not block scope. It's a syntax issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.