DEV Community

Discussion on: 7 Tricks I Use To Rewrite JavaScript Code From My Early Days

Collapse
 
lexlohr profile image
Alex Lohr

#4: you may want to consider using the nullish coalescing operator ?? (use || consciously if you want to catch all falsy types).

#6: for...of is not neccessarily more readable than forEach, especially if you use arrow functions.

#7: you can use Object.assign on DOM elements, e.g.

const myDiv = Object.assign(document.createElement('div'), { id: 'test', className: 'other test' })
Enter fullscreen mode Exit fullscreen mode
Collapse
 
momander profile image
Martin Omander

About "for...of": I find myself using it often with async/await. Foreach() does not work well with asynchronous code. I think this was the author's point.
For more background see this: stackoverflow.com/questions/375766...

Collapse
 
lexlohr profile image
Alex Lohr

It depends. If you want to wait on a list of promises returned by an array of functions, better use await Promise.all(fns.map(fn => fn()), as it will allow you to run them parallel.