logo

NJP

Trailing Commas in Functions - Let's Learn ECMAScript 2021 with Earl Duque

Import · Jan 25, 2024 · video

Okay. Next up is trailing commas in function
definitions. It's a simple one.
Let me show you really quick. So let's say we are creating a function
called the we'll call it createTask and we have a title as a parameter
and we have a description as a parameter. Normally
this is how we would write it, right? But now we can actually have
trailing commas before
if you had put a trailing comma on there and not followed up with a parameter,
you would get an error. But now you can have trailing commas. Likewise when we call a function,
we can also have trailing commas there. So that's allowed now to. Why is this important? Okay, so this is important
because it makes version control differences much cleaner and it makes rearranging parameters
and arguments much easier. And so if I wanted to make this
a little bit more visually appealing
and easier in source control like git, I would actually format it like this. So you could see now I have each of my parameters
on a different line. So if I were to ever edit
what items I have on these parameters, change one, add them, instead of the entire line being highlighted as a difference,
only the separate parameter is. And similarly,
if I do it like this on the actual calling of the function, again, if I make a change
to any of these parameters, only that parameters line is highlighted
in the difference checker on
git instead of the entire line. That's it!

View original source

https://www.youtube.com/watch?v=88qbVbXCIZw