0

I've declared a simple JavaScript object which is the model model of a table.

The code works fine and I can access all the data that i need.

For some reason, if i get Uncaught SyntaxError: Invalid or unexpected token when i write the highlighted attribute (in the next piece of code) with numbers followed by letters:

var parameters_table = {
    M:{D:3.944,T:1.1606,M:5.3893},
    7:{D:5.1847,T:2.059,M:2.8712},
    aug:{D:4.99,T:6.173,M:1.495},
    6:{D:4.962,T:1.2287,M:0.94456},
    aug7:{D:6.14,T:4.16,M:0.632},
    m7b5:{D:6.12226,T:2.6665,M:0.29159},
    7sus4:{D:4.7313,T:2.7964,M:0.2592}//Here there's the error with the name
};

if i write something like:

var parameters_table = {
    M:{D:3.944,T:1.1606,M:5.3893},
    7:{D:5.1847,T:2.059,M:2.8712},
    aug:{D:4.99,T:6.173,M:1.495},
    6:{D:4.962,T:1.2287,M:0.94456},
    aug7:{D:6.14,T:4.16,M:0.632},
    m7b5:{D:6.12226,T:2.6665,M:0.29159},
    sus4:{D:4.7313,T:2.7964,M:0.2592}//This works
};

Just by removing the 7, it works fine.

Why do i get this error? I thought it could be a problem with alphanumeric names, but i've already used mixed characters (numbers + letters) in the other fields without any problems.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
  • 1
    properties cannot begin with numbers https://stackoverflow.com/questions/5809790/can-i-get-a-javascript-object-property-name-that-starts-with-a-number – Sharon Aug 13 '19 at 10:51

5 Answers5

2

JavaScript properties and variable names cannot begin with numbers. You can use a slight hack involving string access like so however:

"7sus4": {...}

And access it like:

parameters_table["7sus4"]
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

If you put quotes around the properties, the code will work fine:

var parameters_table = {
    M:{D:3.944,T:1.1606,M:5.3893},
    7:{D:5.1847,T:2.059,M:2.8712},
    aug:{D:4.99,T:6.173,M:1.495},
    6:{D:4.962,T:1.2287,M:0.94456},
    aug7:{D:6.14,T:4.16,M:0.632},
    m7b5:{D:6.12226,T:2.6665,M:0.29159},
    "7sus4":{D:4.7313,T:2.7964,M:0.2592}//Here there's the error with the name
};
Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20
1

In Javascript if you want to use letter first before with alphabets in the property, You need to make the key to be surrounded in double quotes "" as example Use the following it will work fine

var parameters_table = {
M:{D:3.944,T:1.1606,M:5.3893},
7:{D:5.1847,T:2.059,M:2.8712},
aug:{D:4.99,T:6.173,M:1.495},
6:{D:4.962,T:1.2287,M:0.94456},
aug7:{D:6.14,T:4.16,M:0.632},
m7b5:{D:6.12226,T:2.6665,M:0.29159},
"7sus4":{D:4.7313,T:2.7964,M:0.2592}};

Similarly we need to use "" when there is '-' in the property name

Vivek Singh
  • 655
  • 6
  • 16
1

What went wrong

The names of variables, called identifiers, conform to certain rules, which your code must adhere to!

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). They can't start with a digit! Only subsequent characters can be digits (0-9).

Gives below error:

SyntaxError: identifier starts immediately after numeric literal

More details click below link:

developer.mozilla.org syntax errors

Saurabh Bishnoi
  • 614
  • 6
  • 10
1

Javascript identifiers cannot begin with numbers:

11.6.1 Identifier Names

11.6.1.1 Static Semantics: Early Errors

IdentifierStart :: \ UnicodeEscapeSequence

  • It is a Syntax Error if SV(UnicodeEscapeSequence) is none of "$", or "_", or the UTF16Encoding (10.1.1) of a code point matched by the UnicodeIDStart lexical grammar production.

IdentifierPart :: \ UnicodeEscapeSequence

  • It is a Syntax Error if SV(UnicodeEscapeSequence) is none of "$", or "_", or the UTF16Encoding (10.1.1) of either <ZWNJ> or <ZWJ>, or the UTF16Encoding of a Unicode code point that would be matched by the UnicodeIDContinue lexical grammar production.

- ECMAScript 2015 Sepc

Essentially the IdentifierStart specifies that your key needs to begin with either a $, _, or a letter (valid Unicode escape sequences can also be used). So, when you try and use 7sus4, your key doesn't begin with either of the above and so you'll get a Syntax Error.

The IdentifierPart specifies that you're not only limited to just alphabetic letters, and so you can have numbers within your key. This means your identifier isn't strictly defined to alphabetic keys (allowing you to have a mix of both numeric characters and regular letters eg: a1).

In your case, you can instead make your object key a string, which will allow you to begin your key name with a number:

const parameters_table = {
    ...
    "7sus4": {D:4.7313,T:2.7964,M:0.2592}
};

You can read more about javascript's naming rules here.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    FYI: For those wondering what `SV(…)`—as used in section 11.6.1—means. It is conveniently defined (appearing as `SV` without the `(`) in section 11.8.4.2. [Tip: 8 > (is greater than) 6.] You're supposed to read the whole spec w/o attaching meaning to the terms, then comprehend it later during a mental "link editing" phase or something. Spoiler: It means "String Value" – Colin Fraizer Feb 07 '20 at 00:04