DEV Community

Cover image for 3 simple JS tricks to make life easier..
Muse'
Muse'

Posted on

3 simple JS tricks to make life easier..

I'm sure we all have our styles of writing code and are comfortable with but change and learning new ways to write code is essential as software engineers...

Here are some of the simple and clear tricks you can use to code faster!

Format your JsonData:

Many developers when using JSON.Stringify, use the first(required param) = value but in reality, there is 2 other optional params: replacer & space

Let me show you how it works and you'll understand exactly what i mean!

1st Step: Construct your JSON Data

Alt Text

2nd Step: Format it
We want to format it by first name and last name for each reader, and we want to add a tab to each level.
This how you'd write that:
Alt Text

3rd Step: console.log/render that formatted JSON!
Alt Text

Drop the Array.Filter() and rely on Set():

Make use of the native Set() object rather than filtering through the array to get unique values.
Alt Text

Incorporate some spread operating in your code:

Sometimes we all need to merge classes or objects together, spread operator can easily get the job done.
Alt Text
The code above will combine all those objects into one object!

Conclusion

ES6 has not only made JavaScript more efficient but also more fun, So if you haven’t taken the time to play around, you definitely should.
Let me know below in the comments if you have any favorite code tricks that you'd like to share :)

Top comments (0)