Today I Learned - Rocky Kev

TIL trailing commas in Javascript

POSTED ON:

TAGS:

Trailing commas in Javascript is a interesting feature.

JavaScript has allowed trailing commas in array literals since the beginning, and later added them to object literals, and more recently, to function parameters and to named imports and named exports.

One thing to note:

JSON, however, disallows trailing commas.



// this is fine
const list = ['ace', 'luffy', 'sabo']

// this is also fine
const list = ['ace', 'luffy', 'sabo', ]

On small lists, it's cleaner to just remove it. But when working with large arrays/objects/params, it starts to get really difficult.

For more specific details and use-cases, check out the MDN link above!

Finally - don't argue about this. Your linter should be handling if this rule is appropriate or not.


Related TILs

Tagged:

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL How to steal localData using an XSS attack

But that's just a red flag that opens the door to bigger issues.

TIL Clear-Site-Data

The Clear-Site-Data header clears browsing data (cookies, storage, cache) associated with the requesting website. It allows web developers to have more control over the data stored by a client browser for their origins.