Skip to content Skip to sidebar Skip to footer

How To Access Object Property Beginning With A Number (SyntaxError: Unexpected Identifier)

I have an object within another object, which im trying to get the value but it always returns 'unexpected identifier'. snow: Object {3h: 1.3} console.log(data.snow.3h) //returns

Solution 1:

data.snow['3h'];

Properties accessed with dot notation can't begin with a number.

snow: Object {3h: 1.3} could be refactored to snow: {3h: 1.3}. It is redundant to type Object.

Also, if you wrap your property names in quotes, you can use bizarre property names like:

var myObj = {
  '^': 'foo'
};
console.log(myObj['^']);

but, I generally stick to more standard names that I can access with dot notation.


Post a Comment for "How To Access Object Property Beginning With A Number (SyntaxError: Unexpected Identifier)"