10 Best JavaScript Development Practices You Need to Know

10 Best JavaScript Development Practices You Need to Know

Many extended features of Javascript make it more powerful, and also give programmers the opportunity to create more beautiful and user-friendly websites.

Although many developers are happy to praise JavaScript, some still see its dark side.

Web pages that use a lot of javascript code will load slowly, and excessive use of javascript makes web pages ugly and clunky. Soon, how to use javascript effectively became a very hot topic.

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

1. Keep the code as simple as possible

You may have heard this code conciseness issue many times. As a developer, you may have used it many times in your code development process, but don't forget this in js development.

  • Try to add comments and spaces in development mode to keep the code readable.
  • Please remove spaces and comments before publishing to the production environment, and try to abbreviate variable and method names.
  • Use third-party tools to help you compress JavaScript.

2. Think before you modify your prototypes

Adding new properties to an object's prototype is a common cause of script errors.

  1. yourObject.prototype.anotherFunction = 'Hello';
  2. yourObject.prototype.anotherMethod = function () { … };

In the above code, all variables will be affected because they all inherit from yourObject. Such usage may lead to unexpected behavior. So it is recommended to delete similar modifications after use.

  1. yourObject.prototype.anotherFunction = 'Hello';
  2. yourObject.prototype.anotherMethod = function () { … };
  3. test.anotherMethod();
  4. delete yourObject.prototype.anotherFunction = 'Hello';
  5. delete yourObject.prototype.anotherMethod = function () { … };

3. Debugging Javascript Code

Even the best developers make mistakes. To minimize the chances of such mistakes, run your code in your debugger to make sure you don't run into any subtle errors.

4. Avoid Eval

Your JS will work just fine without the eval method. eval allows access to the javascript compiler. If a string is passed as an argument to eval, its result can be executed.

This can significantly reduce the performance of your code. Try to avoid using eval in a production environment.

5. Minimize DOM access

DOM is the most complex API and will slow down the code execution process. Sometimes the web page may not load or load incompletely. ***Avoid DOM.

6. Learn JavaScript before using JavaScript libraries

The internet is flooded with many javascript libraries, and many programmers tend to use js libraries without understanding the negative impact. It is strongly recommended that you learn basic JS code before using third-party libraries, otherwise, you are ready for bad luck.

7. Do not use "SetTimeOut" and "Setinterval" methods as an alternative to "Eval"

  1. setTimeOut( "document.getID('value')" , 3000);

In the above code, document.getID('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 reducing performance. Therefore, it is recommended to pass a method in these methods.

  1. setTimeOut(yourFunction, 3000);

8. [] is better than new Array();

A common mistake is to use an object when an array is needed or an array when an object is needed. But the principle is simple:

"When property names are small consecutive integers, you should use an array. Otherwise, use an object" - Douglas Crockford, author of JavaScript: Good Parts.

suggestion:

  1. var a = [ '1A' , '2B' ];

avoid:

  1. var a = new Array();
  2. a[0] = "1A" ;
  3. a[1] = "2B" ;

9. Try not to use var multiple times

When initializing each variable, programmers are accustomed to using the var keyword. Instead, it is recommended that you use commas to avoid redundant keywords and reduce code size. As follows:

  1. var variableOne = 'string 1',
  2. variableTwo = 'string 2',
  3. variableThree = 'string 3';

10. Don’t ignore the semicolon ";"

This is often one of the reasons why people spend hours debugging.

I’m sure you’ve read about this in other articles, but there are a lot of basic rules that people tend to overlook. Have you ever overlooked a semicolon? Have you ever encountered performance issues with the eval keyword?

I hope you like it, thank you!

<<:  Teach you step by step how to view nearby people

>>:  Google open-sources Eddystone BLE beacon communication format to challenge Apple

Recommend

Introduction to Bidding Ads and Access Industries at Station B

Introduction to B station advertising Bilibili ad...

How to place ads on Kuaishou? HOW DOES IT PERFORM?

Kuaishou is one of the few short video communitie...

Analyze the marketing strategy of maternal and infant brands!

With the implementation of the "Three-Child ...

Qualcomm and Huawei are increasingly competing for the first 5G chip

Qualcomm, which was overshadowed by Huawei's ...

How to analyze the small details of Baidu bidding?

Baidu bidding has become a well-known means of In...

Community operation: ten active cases of refined community operation

“Any social network operation that does not obser...

A complete analysis of the Toutiao search account setup and delivery ideas

As a new search platform launched this year, Tout...

Tablets have no future: iPad replacing PC is a false proposition

Did you notice this detail at the Apple new produ...

These Beijing alleys are full of celebrity stories

Beijing's hutongs were formed in the Yuan Dyn...