"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > avascript Tips You Should Know

avascript Tips You Should Know

Published on 2024-11-06
Browse:436

Written by Joab Chua

1. Console.log

  • Add colour to your console log

Stop doing just this! ❌

avascript Tips You Should Know

avascript Tips You Should Know
Try this instead. ✅

avascript Tips You Should Know

avascript Tips You Should Know
But if you have an array of objects, try this would be even better ?

avascript Tips You Should Know

avascript Tips You Should Know
If you want to measure how fast certain operations run in your code, try this.

avascript Tips You Should Know

avascript Tips You Should Know
Do console.time and console.timeEnd to measure the time taken in the browser console.
Lastly, if you want to trace your code and its execution sequence, try this.

avascript Tips You Should Know

avascript Tips You Should Know
You will be able to see tonnes of info that you need to debug your application.

2. Object de-structuring

avascript Tips You Should Know
Did it happened to you before that you only need a few variables in the object but you are being passed the full object and using it in this conventional way?

avascript Tips You Should Know
Repeatedly using the human object declaration in your function? Don’t do this. Use ES6 object de-structuring instead. ✅

avascript Tips You Should Know
De-structure the variables that you need to use in the function arguments. Alternatively, you can de-structure inside the function itself.

avascript Tips You Should Know

3. Template Literal

❌ Don’t do this when you are trying to piece your variables with string.

avascript Tips You Should Know
Do this instead. ✅
avascript Tips You Should Know

4. Spread operator

Let’s say you have 2 object and you want to combined them or assign some of the properties of 1 object to another. Traditionally, we will do this.
avascript Tips You Should Know
There is technically nothing wrong with that, except that you are actually mutating your object. Try this instead. ✅
avascript Tips You Should Know
Best thing of spread operator is that it works for array as well. ?
Instead of this.
avascript Tips You Should Know
Do this instead.
avascript Tips You Should Know

5. loops

Are you guilty of doing for loop this way to calculate the total amount of the cost in an array? Stop doing this. ❌
avascript Tips You Should Know
Start to change your style to this instead.
avascript Tips You Should Know
Clean and lean. Job done! ✅

Summary

Hope that these few tips will guide you along to write better, cleaner and leaner code.

Sponsored by MarsCode
Welcome to join our Discord to discuss your ideas with us.

avascript Tips You Should Know

MarsCode

MarsCode provides an AI-powered IDE and an AI extension to assist your programming tasks.
Release Statement This article is reproduced at: https://dev.to/marscode/5-javascript-tips-you-should-know-23db?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3