fbpx

JavaScript Best Practices- Part 2

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

1. The Fastest Way to Build a String

Don’t constantly reach for your handy-dandy “for” report when you essential to loop finished a grouping or object.

Using native methods (like join ()), irrespective of what’s going on behind the concept layer, is typically much quicker than any non-native alternate.

2. Reduce Globals

By decreasing your global trail to a single name, you considerably decrease the chance of bad dealings with additional applications, widgets or libraries.

Better

Notice how we have “reduce our footprint” to just the unreasonable named “DudeNameSpace” objective.

3. Comment Your Code

It might appear excessive at first, but trust, you WANT to comment your code as best as possible. What occurs when you arrive to the project months later, only to discover that you can’t simply recall what your line of intellectual was, or what if one of your associates wants to study your code?

It is always recommended to comment essential sections of your code.

4. Avoid Mixing with Other Technologies

JavaScript is good for calculation, adaptation, and admittance to outside sources (Ajax) and to express the performance of an interface (event handling). Everything else should be saved to the technology we have to do that job.

FOR EXAMPLE:

Place a red border around all fields with a class of “mandatory” when they are blank.

..Two months down the line: All styles have to conform to the new company style guide, no borders are permitted and mistakes should be shown by an alert icon next to the component.

People shouldn’t have to change your JavaScript code to change the look and feel.

Using CSS legacy you can escape having to loop over a lot of fundamentals