Home  >  Article  >  Web Front-end  >  avascript Tips You Should Know

avascript Tips You Should Know

王林
王林Original
2024-08-19 17:01:37388browse

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.

The above is the detailed content of avascript Tips You Should Know. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Problem Solving PatternsNext article:Problem Solving Patterns