JavaScript Best Practices- Part 3

Blog >> Javascript Best Practices >> JavaScript Best Practices- Part 3

1. Modularize

Keep your code modularized and specific.

It is attractive and informal to write one function that does the whole thing. However, as you spread the functionality you will find that you do the same things in numerous functions.

To avoid that, make sure to write minor, general helper functions that achieve one precise task rather than catch-all methods.

At a later phase you can also depiction these functions when spending the revealing element pattern to generate an API to spread the main functionality.

Decent code should be easy to shape upon without reworking the core.

2. Enhance Progressively

Avoid generating a lot of JavaScript dependent code.

DOM group is relaxed and exclusive.

Fundamentals that are reliant on JavaScript but are accessible when JavaScript is turned off are a broken promise to our users.

3. Allow for Configuration and Translation

Everything that is possibly to change in your code should not be dispersed through your code.

This comprises labels, CSS classes, IDs and presets.

By placing these into a formation object and creating this one public we make maintenance easy and permit for customization.

For example:

4. Always, Always Use Semicolons

Technically, most browsers will allow you to get away with omitting semicolons.

Having said that, this is an actual immoral practice which can possibly lead to much larger, and harder to find, matters.


5. “For in” Statements

When looping complete stuffs in an object, you might find that you’ll also recover technique tasks as well. In order to work about this, continually wrap your code in an if statement which filters the information.