0

In the following statement:

let x=[1,2,3];
console.log(x.0); // should be console.log(x[0])

This evaluates to an error:

SyntaxError: missing ) after argument list

I have two questions about this:

  1. For array syntax, is the only way to reference an element using arr[idx] syntax and not arr.idx?
  2. Why does it give the above error and not something like, "property 0 not valid identifier"?
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • 2
    1. Yes. 2. Because it's a syntax error. By definition the syntax parser doesn't know *what* you want to do. Because the syntax you've used is meaningless. So, the code is broken. As computers are not equipped with mind reading capabilities *yet*, they can only deduce that it's broken code as it fails to satisfy some rules, not what the original thought was. Computers are great at doing what you tell them to, rather than what you intend for them to do. – VLAZ Oct 09 '21 at 20:56
  • @VLAZ understood regarding #2 -- but what does the message think the error is? For example, why does it say missing `)` ? – carl.hiass Oct 09 '21 at 20:57
  • 1
    Because it breaks while inside the `()` of `console.log()` and the parser expects the closing bracket but it seemed to it that it wasn't closed. Parsing broke down while inside the brackets. – VLAZ Oct 09 '21 at 21:00
  • 1
    https://stackoverflow.com/a/16908744/212869 – NickSlash Oct 09 '21 at 21:04

0 Answers0