DEV Community

Discussion on: What are the tips or techniques you wish someone had told you ages ago?

Collapse
 
nickyhajal profile image
Nicky Hajal

One little one for JS...

If you want to debug code like this:

const filtered = someArray.filter((elm) => elm.tags.includes('someTag'))

And it isn't working, so you want to add a log to see what's going on. But it's a pain because you think you need to build out the entire function body like this:

const filtered = someArray.filter((elm) => {
  console.log(elm.tags);
  return elm.tags.includes('someTag');
})

Which is also annoying because once it works, you need to remove all that extra syntax.

Instead, you can use a comma, like this:

const filtered = someArray.filter((elm) => console.log(elm.tags),elm.tags.includes('someTag'))

JS will evaluate both statements and send the value of the final one.

It's a little thing, but makes my life a little nicer on a daily basis :)