Top 10 JavaScript Tips & Tricks #11
barbarbar338
announced in
Posts
Replies: 1 comment 1 reply
-
hi how are you? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Top 10 JavaScript Tips & Tricks
In this post, I'm going to show you 10 awesome JavaScript tips & tricks that I've learned from over 10 years of experience to make your life easier. And it will help you become a better developer. So if you're interested, keep reading.
1 - Stop Using "data" (Or Something Random) As A Variable Name
Be a little more descriptive when naming your variables. In the example above, of course, we can see at a glance that the "data" variable is the sum of numbers, but suppose this "data" nomenclature occurs in more than one place in a larger project. Everything is mixed up!
Use meaningful prefixes when naming your variables:
get
prefix to the name of this function. For examplegetUser()
set
orupdate
. For exampleupdateUser()
is
,should
,can
to the beginning. For examplecanSpeak = true
In short, people should understand what your named variables mean in one reading.
2 - Pass Arguments As An Object Rather Than One At A Time
Passing arguments as objects in functions is a logical move in many ways:
Below is an example of bad use of arguments.
As you can see, in this example we already have an object as a whole, but we need to pass all the elements of this object as arguments one by one.
3 - Use Built-in Features
JavaScript offers you quite a lot of features. Using these built-in features instead of creating a new function immediately will save you time and allow you to focus on your main project faster. When looking for a solution to your problem, you can use existing functions instead of creating a new function.
4 - Removing Duplicates From An Array
This trick is pretty simple. Suppose I have a string containing numbers, strings and booleans. And I want to make sure there are no duplicate items in this array. I can create a simple Set for this and convert it to an array with the spread operator.
5 - Use Generators To Create Sequential IDs
Generators introduced with ES6 are a very useful feature that allows us to create not similar repetitive sequences.
If you don't know what generators are, they are functions that employ lazy evaluation by making use of the yield keyword to process and return data, on-demand.
It may look like generator functions will burn CPU cycles in an infinite loop, however, generators describe a state machine, allowing transitions to forward states to occur through provided code (through subsequent yields). These transitions occur on-demand whenever the next method is called, hence the term lazy evaluation!
With this trick, you no longer have to rely on global/class-scoped variables to remember the state!
6 - Waiting For Multiple Promises To Resolve
If you need to wait for multiple asynchronous processes to finish before performing another action, do not wait for them one at a time. You can use the
Promise.all(promiseLike[])
method to run all asynchronous operations at the same time and continue your operation after they are all finished.7 - Format JSON Output With Spaces
JSON.stringify(object, replacer, indent)
A simple yet very effective function for exporting readable JSON by supplying the amount of spaces to use for indentation in the third parameter.
The second parameter is the replacer and it can either be a function which controls the stringify-ing process, or it can be an array, in which case it indicates the name of the properties that should be included in the stringified output.
8 - Use The Optional Chaining Operator
With optional chaining being supported in most browsers, it is now easier to parse complex objects.
Previously, developers would resort to using either short-circuits or nested if statements in which they would compare against undefined.
Now, it is even easier (and cleaner!) to accomplish the same validation with the optional chaining operator.
Even better, you can even use optional chaining with expressions using bracket notation, or, if you have a deeply nested object, you can stack optional chaining operators to check for deeper properties.
9 - Be Modular
Do not create the functions just to serve a purpose, try to handle multiple tasks with a single function. You don't need to create a special function for everything you need to do. Just use the old one.
10 - Time the execution of your code
A priceless function for developers seeking to deliver high-performance code, the time method takes a timer name as parameter and expects to be met with a call to timeEnd in which the same timer name is supplied.
The timeEnd method prints the elapsed time in milliseconds between the two function calls and it allows programmers to quickly observe the bottlenecks of their code and refactor with ease.
This approach is much better than calculating the elapsed execution time manually, as it is built-in and widely supported across modern browsers.
Thanks For Reading
Thank you for taking your precious time to read this post. If you want to learn more JavaScript tips & tricks, I recommend you to visit my blog often.
Beta Was this translation helpful? Give feedback.
All reactions