Draked
Draked

Reputation: 3

Lists, and an unknown error

function testFunction() {
    var test = {
      1: 'test1',
      2: 'test2',
      3: 'test3'
    };
  Logger.log(test.1);
}

I'm getting the following error and I cannot figure out why. I want it to log: "test1".

Missing ) after argument list. (line 7, file "Code")

Upvotes: 0

Views: 39

Answers (2)

Chris Cirefice
Chris Cirefice

Reputation: 5805

In JavaScript (and probably most other languages), identifiers cannot start with an integer. This is because integer literals (i.e. 1, 1024, 42, etc) would not be able to be parsed. So, the following line is your problem:

Logger.log(test.1);

throws an error because you are trying to access the identifier 1 on your test object. Since you use an integer literal, that's what the lexer finds, and so you get that error. You need your identifiers to be strings, and strings only.

Upvotes: 1

Functino
Functino

Reputation: 1935

That's not a list, it's an object. Objects in Javascript coerce their keys to strings. What you probably want is an array, like so:

function testFunction() {
    var test = ['test1', 'test2', 'test3'];
    Logger.log(test[0]);
}

Note that arrays are 0-based, and the use of brackets.

Upvotes: 0

Related Questions