JS Developers Level
To help you become a better JS developer, we’ll cover typical errors (or bad practices) and their fixes in this post.
Confusing the operators for assignment (=) and equality (==, ===)
The assignment operator(=) assigns values to variables, as its name suggests. It is frequently confused by developers with the equality operator.
The string “nodejs” and the name variable are not contrasted in this scenario. As an alternative, “nodejs” is assigned to name and printed to the console.
The proper comparison method for the code above is as follows:
The double equals comparison operator performs a loose comparison, whereas the triple equals comparison operator performs a tight comparison.
Only the values are compared in a loose comparison. However, the values and datatype are contrasted in a precise comparison.
It is more clearly explained by the code below:
Expecting Synchronous Callbacks
They are not synchronous callbacks. When a delayed execution is finished, they are used as a function to be called after an operation.
The global setTimeout method is a good illustration of this. It takes a callback function as its first parameter and a duration (in ms) as its second argument as follows:
The callback function is called 300 ms later. The rest of the code, however, executes before it is finished. The most recent console.log was executed first for this reason.
Developers frequently view callbacks as synchronous, which is a common error. As an illustration, consider a callback that returns a value that can be used in other processes.
Here is the error:
Wrong References To This
Here’s an illustration of how to use this incorrectly:
Disregarding Object Mutability
The reference to the location in memory where the item is stored is shared by objects 1 and 2.
Any attempt to access the original properties would result in an error or return undefined when this happened.
Saving Arrays And Objects
When items are preserved this way, accessing them is challenging. Accessing the object in the example with .name would lead to an error. This is because [Object Object] is now a string and lacks a name property.
Using JSON.stringify (for turning objects into strings) and JSON.parse (for parsing JSON) is a better technique to save objects and arrays in local storage (for converting strings to objects). Accessing the objects is made simple in this method.
Improper Variable Naming
Yes, this error is still made by coders. Although the naming is difficult, developers have little other option. Programming best practices call for both name variables and comments.
It’s fine with the variable discount, but what about p or total? The sum of what? As an improvement to the above, how about:
Because a developer may never be the sole developer working on a codebase at a certain time or in the future, properly identifying variables is crucial.
Correct variable naming will simplify for contributors to comprehend how a project operates.
As demonstrated in the code above, checking Boolean values is standard procedure. This is fine; however, when specific numbers are tested, errors start to appear.
Of course, there are mistakes (some minor, some significant) in addition to those mentioned above. So make sure you keep up with language advancements.
By learning from and avoiding these pitfalls, you may create stronger and more dependable web tools and applications.