DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Quick JavaScript tip: trailing commas inside an object literal

Quick JavaScript tip: trailing commas inside an object literal

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Jun. 21, 11 · Interview
Like (0)
Save
Tweet
Share
4.59K Views

Join the DZone community and get the full member experience.

Join For Free

It used to be that some JavaScript engines weren’t picky about trailing commas inside an object literals, while others threw a syntax error. The ECMAScript 5 language specification [1] has made trailing commas legal, via the following syntax rule (in Sect. 11.1.5):

    ObjectLiteral :
        {}
        { PropertyNameAndValueList }
        { PropertyNameAndValueList , }
You can use this syntax in all modern browsers [3]. It makes it easier to maintain the content inside object literals, especially long ones: You don’t have to keep track which property is last when rearranging things. Take the following example.
    var obj = {
        foo: 123,
        bar: function () { ... },
    }
Thanks to the trailing comma, you save two steps when swapping foo and bar, as there is no need to add a comma after bar or to remove the comma after foo.

Related reading:

  1. Standard ECMA-262: ECMAScript Language Specification
  2. What’s new in ECMAScript 5
  3. ECMAScript 5 compatibility table

 

From http://www.2ality.com/2011/06/object-literal-comma.html

Literal (computer programming) Object (computer science) JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Custom Validators in Quarkus
  • HTTP vs Messaging for Microservices Communications
  • Monolithic First
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: