SoFunction
Updated on 2025-03-03

10 Best JavaScript Development Practice Summary You Need to Know

Although many developers are happy to celebrate JavaScript, there are still people who see the dark side of it.

Web pages that use a lot of javascript code will load very slowly, and excessive use of javascript makes the web page ugly and drag-out. How to use javascript effectively soon became a very hot topic.

Here let's list 10 best javascript practices to help you use javascript effectively.

1. Keep the code as simple as possible
Maybe everyone has heard of this code concise problem N times. As a developer you may have used it many times during your code development, but don't forget this in js development.

Try to add comments and spaces in development mode to keep the code readable
Please delete spaces and comments before publishing to the product environment and try to abbreviate variables and method names
Use third-party tools to help you implement compressed javascript.
2. After thinking, modify prototypes
Adding new properties to the object prototype is a common cause of script errors.
Copy the codeThe code is as follows:

= ‘Hello';
= function () { … };

In the above code, all variables will be affected because they are inherited from "yourObject". Such use can lead to unexpected behavior. Therefore, it is recommended to delete similar modifications after use.
Copy the codeThe code is as follows:

= ‘Hello';
= function () { … };
();
delete = ‘Hello';
delete = function () { … };

3. Debug Javascript code
Even the best developers make mistakes. To maximize the reduction of similar errors, please run your code in your debugger to confirm that you have not encountered any subtle errors

4. Avoid Eval
Your JS works well without the "eval" method. "eval" allows access to the javascript compiler. If a string is passed as an argument to "eval", its results can be executed.

This will greatly reduce the performance of the code. Try to avoid using "eval" in product environments.

5. Minimize DOM access
DOM is the most complex API, which can slow down the code execution process. Sometimes the web page may not be loaded or incompletely loaded. It is best to avoid DOM.

6. Learn javascript before using javascript class library
The Internet is full of many JavaScript class libraries, and many programmers often use the js class library without understanding the negative impact. It is strongly recommended that you learn basic JS code before using third-party library, otherwise you will be prepared to be in trouble.

7. Do not use the "SetTimeOut" and "Setinterval" methods as alternatives to "Eval"
setTimeOut( "('value')", 3000);
In the above code ('value') is processed as a string in the "setTimeOut" method. This is similar to the 'eval' method, which executes a string in each code execution, thus degrading performance, so it is recommended to pass a method in these methods.

setTimeOut(yourFunction, 3000);
8. []Better than "new Array();"
A common mistake is to use an object when an array is needed or an array when an object is used. But the principle of use is simple:

"When the property name is a small continuous integer, you should use an array. Otherwise, use an object" - Douglas Crockford, Author of JavaScript: Good Parts.

suggestion:
var a = ['1A','2B'];
avoid:

var a = new Array();
a[0] = "1A";
a[1] = "2B";
9. Try not to use var multiple times
When starting each variable, programmers are used to using the "var" keyword. Instead, it is recommended that you use commas to avoid unnecessary keywords and reduce the size of your code. as follows:

var variableOne = ‘string 1',
variableTwo = ‘string 2',
variableThree = ‘string 3';
10. Don't ignore the semicolon ";"
This is often one of the reasons why everyone spends hours debugging.

I'm sure you've read the above related content in other articles, but you may often ignore many basic rules. Have you ever ignored the semicolon? Have you encountered performance problems with eval keyword problems? I hope everyone likes it, thank you!