These clever JavaScript tricks are essential for every developer
In the past 5-10 years, JavaScript's market share has risen to new heights, especially since ES6 launched in June 2015. This made JavaScript the most popular web-development programming language, allowing developers both to create front-end UI as well as back-end logic.
It has been the most popular choice for web application development due to the numerous JavaScript frameworks it has released in recent years. These are the top JavaScript frameworks:
This is why we are going to show you some simple tricks that will improve the readability and sanity of your code.
As objects, passing arguments This method of passing off arguments has many benefits.
It doesn't matter what order you use, so it is possible to focus on writing high-quality code and not constantly checking the function definition.
The IDE will automatically complete auto-completion based on the argument you provide.
This communicates intent clearly as function calls specify each property's value.
The addition of verbosity will be a huge benefit to large codebases.
Generators are used to generate sequential IDs Generators have been introduced in ES6. This makes it easier to generate infinite, non-repeatable sequences.
Generators can be described as functions that use lazy evaluation. They make use of the yield keyword to process return data and produce them on-demand.
Although it may seem like generator functions will consume CPU cycles in an endless loop, generators are actually a description of a state machine that allows for transitions to forward states through provided code (through subsequent yields). These transitions happen on-demand when the next method is called. This is why lazy evaluation is used!
This trick will allow you to forget about global or class-scoped variables.
Format JSON output using spaces This tool is simple and effective for exporting readable JSON. You can specify the number of spaces you want to indent in the third parameter.
The replacer parameter is the second parameter. It can either be a function that controls stringify-ing or an array in which case it specifies the name of properties to be included in the stringified output.
Learn more about stringify here.
You can use the optional chaining operator It is easier to understand complex objects now that optional chaining is supported by most browsers.
Developers used to resort to short-circuits and nested statements if they wanted to compare undefined with their code.
It is now even simpler (and easier!) You can also use the optional chaining operator to perform the same validation.
You can also use optional chaining using expressions using bracket notation. If you have a deeply nestled object, you can stack optional chainsing operators to verify for deeper properties.
Operators can be useful in many other situations. The documentation contains more information.
Use the destructuring assignment syntax Another easy way to extract the most important information from JavaScript objects is using the Quick and Easy Trick.
Developers can quickly extract values from arrays and properties from objects using the destructuring syntax.
This syntax can be used to perform multiple tricks, including variable-swapping one lines or parsing only the relevant properties of a returned object.
The documentation contains more information about the syntax of the destructive assignment syntax.
Spread operator is used to create shallow copies of objects (and arrays) The spread syntax has been introduced in JavaScript. This makes it easier than ever for objects and arrays to be expanded or copied.
This is particularly useful when you need to manage state in React or React native. All you have do is to copy the current state using the object literal, modify the properties you choose, and then change the state with useState's state hook.
It's also an excellent way to concatenate arrays or merge objects with one-liners, rather than having to iterate through each instance and merge manually.
Learn more about spread syntax and how it is used here.
This simple, yet effective way to remove duplicates from an array is using a single-liner.
We have used the spread operator, which was just demonstrated to expand the set and build an array in this example.
This trick works well with any type of value and even some JavaScript odd equality behaviors.
Sets can also be used to remove duplicates in complex objects.
Learn more about Set and its uses here.
Wait until multiple promises are fulfilled This trick is invaluable when you have multiple tasks to run simultaneously and need to wait for them all to complete. Each task can be run simultaneously, so they can all be processed simultaneously. Once all promises have been fulfilled, the returned data can also be used.
Keep in mind that Promise.all will immediately return any promise that is not fulfilled.
This trick is useful if you're developing micro-services architectures and need to quickly fetch non-sequential, important data from multiple endpoints.
Promise.all will fulfill all of its promises once they are fulfilled.
As with all promises, you can also set a callback to the next step in order to process the results.
Mozilla's documentation has more information about promises and how they are made.
Casting values in arrays using map This is one of the most straightforward tricks in this article, but it provides a very elegant way to cast an array of numerical value represented as strings into JavaScript number (All JavaScript number are 64-bit floating-point numbers).
This uses the map method for Array. By passing Number as a parameter, it will call the constructor for Number for each value in the array and return the result.
Time the execution The time method is a valuable tool for developers who want to write high-performance code. It takes a timername as a parameter and expects to receive a call from timeEnd where the same timer name was supplied.
The timeEnd method prints out the time between two function calls in milliseconds. It allows programmers to quickly see bottlenecks in their code and then refactor easily.
This is a much more efficient way to calculate the time between executions than manually. It is also supported by most modern browsers.
More information about the console's incredible methods can be found here.
Follow Law Law Helps Prefer Law Allow Law Guide Law Final Law Law Professional Law Sure Daily Law Study Values Law Law Life Time Happy Life Law Flowing Law One Life Law Law Success Law About