Home  >  Article  >  Web Front-end  >  How to Access Object Properties with Numeric Names in JavaScript?

How to Access Object Properties with Numeric Names in JavaScript?

DDD
DDDOriginal
2024-11-02 02:31:30594browse

How to Access Object Properties with Numeric Names in JavaScript?

Referencing Object Properties with Numeric Names

Despite the documentation's suggestion that object literal property names can be integers, accessing these properties using the dot syntax (e.g., me.123) fails.

Alternative Syntax: Array-Style Access

To access an object property with an integer name, you must use the array-style syntax:

  • me[123]

This syntax behaves as if the property were an element of an array, with 123 being the index.

String Notation

Alternatively, you can use string notation to access the property:

  • me["123"]

This approach encloses the integer in double quotes, treating it as a string.

Example:

In your example, you can access the property with integer name 123 using:

console.log(me[123]); // Output: 26

The above is the detailed content of How to Access Object Properties with Numeric Names in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn