in javascript bestPractices frontEnd ~ read.
Best Practices JavaScript !!!

Best Practices JavaScript !!!

Something about JavaScript:

JavaScript is the programming language of HTML and the Web. It makes computers(browser) do what you want them to do. JavaScript is client side scripting language. It helps to make your web application interactive, allows you to create highly responsive interfaces that improve the user experience and provide dynamic functionality, without having to wait for the server to react and show another page. JavaScript is loosely typed. A variable can contain different data types, and it can change its data type, called dynamic data type in JavaScript.

Best Practices:

1. Always declare local variables. All variables used in function must be local variables.

2. Avoid global variables, because global variables and functions can be overwritten by other scripts. Use local variables instead, and use closures majorly. It makes it possible for a function to have “private” variables and can be accessed by another function outside safely.

3. Use === comparison instead ==, == comparison operator always converts type (to matching types) before comparison. The === comparison forces of values and type.

4. JavaScript function does not perform any checking on parameter values (arguments), so always perform safe check on function parameters inside function to avoid run-time errors and to avoid JavaScript to break while executing.

5. Switch statements always use strict comparison so always take care of writing case statements keeping this thing in mind. Always end your switches with Defaults even if you think there is no need for it.

6. Inner functions can be used in JavaScript to achieve encapsulation. Inside functions are private functions to its parent, so won’t expose to all members outside unless needed.

7. It is good practice to declare all variables at top of each script or function, will give cleaner code, provide a single place to look for local variables , make it easier to avoid unwanted global variables, reduce possibility of unwanted re-declarations.

8. Initialize variables when declared, it avoids undefined values. Use {} instead of new Object(), Use “” instead of new String(), Use 0 instead of new Number(), Use false instead of new Boolean(), Use [] instead of new Array(), Use /()/ instead of new RegExp(), Use (){} instead of new function().

9.Beware of automatic(dynamic) type conversion, test.

10. Avoid using eval(). It is used to run text as code, because it allows arbitrary code to be run, it also represents a security problems.

11. JavaScript try to complete the statement by reading the next line, so always end your statement whenever needed with ; to avoid this.

12.Always remember Undefined is not Null in JavaScript, so always add safe check to avoid issues, if necessary, check with typeof.

13. Naming conventions: Use camel-case for variables and functions, pascal-case for class/constructor,capitalize-case for enums, for namespaces use dot separator for different words in name(module).

14. Use use strict it helps you make fewer errors, it catches some common coding bloopers, throwing exceptions. It prevents, or throws errors, when relatively “unsafe” actions are taken (such as commonly made mistake of gaining access to global object). Also note that you can apply strict mode to the whole file or you can use for specific function.

15. Reduce activity in loops, reduce DOM access in JavaScript, delay JavaScript by putting it at bottom of html page(body). It will help in case your JavaScript has errors, still your UI will show up without interrupt.

16. Use IIFE (Immediately-Invoked Function Expression) it executes immediately after it is created. The function is executed right after creation, not after it is parsed, if IIFE is inside a function then it won’t be executed until the function is called.

17. Avoid checking for length inside “for loop”, store it in local variable and use that variable. if you check length inside “for” syntax then it will execute for every loop.

18. Use forEach instead of “for” in case of object traversing.

19. Avoid using timeouts unless it is seriously needed. In case of setInterval don’t forgot to use clearInterval after it’s use.

20. Last but foremost don’t forgot to modular your JavaScript code, it helps to maintain code easily and let you have standard pattern followed across code.