1. Comment Your Code
2. Use === Instead of ==
3. Limit the Use of Global Variables
Declare your variables with var before assigning them any values. After declaring the variable, you can control whether to put it in the global or function-level scope. Try to reduce the number of global variables in your code, as it helps limit unintended interactions with other libraries, applications, and so on.
4. Scripts Should be at the Bottom
5. Avoid Using eval()
The eval() method allows the execution of any code at runtime. Since it gives access to the compiler, the method significantly decreases the security and performance of the application. Replacing eval() with other approaches can help secure the code from injection attacks and sloppy execution.
6. Use [splice] Instead of [delete]
The delete method is good to delete a property of an object. To remove an item from an array, it’s better to use splice. When delete is applied for this purpose, it replaces the object that you want to remove with the value undefined instead of deleting it from the array completely.
8. Look Out for Reserved Words
In addition to keeping the case in check, you should remember to avoid using reserved keywords as variable names. Examples include while, for, if, as well as many functions and object properties, such as toArray() and others.
9. Don’t Omit Semicolons
10. Use Raw Code when Applicable
11. Use map() to Loop Through an Array
To iterate over an array, use the map() method instead forEach(). Unlike forEach(), map() returns a new array.
12. Look Out for Issues with the Floating Point
13. Beware of the [with] Statement
The with statement allows developers to access deeply nested objects and insert new ones at the front of the scope chain. This can frequently result in errors. Instead, use var to help avoid bad behavior by creating references to reused objects.
14. Watch out for SetTimeOut() and SetInterval() Errors
Some developers make the mistake of passing a string value to functions SetInterval() and SetTimeOut() as a parameter. You should never pass a string to these functions. Such code will functionally run like eval() does, compromising security, and it’s also inefficient. Instead, always pass a function name.
15. Avoid Inverting Variable Type
var test = “apple”;
test = 5;
No browser would object to running this code properly, but this practice is dangerous as it can produce substantial bugs in large applications.
An author of Namaste UI, published several articles focused on blogging, business, web design & development, e-commerce, finance, health, lifestyle, marketing, social media, SEO, travel.
For any types of queries, you can contact us on email@example.com.