Home > Web Front-end > JS Tutorial > body text

How to Access Object Properties with Numeric Names in JavaScript?

DDD
Release: 2024-11-02 02:31:30
Original
594 people have browsed it

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!