Today lots of websites, applications are taking advantage of using javaScript functionality and making rich UI websites with the JavaScript or its frameworks.But every time UI developer fights in taking care of the best practice while writing the JavaScript for the project which lead to the performance issues, bad code issues and many more.
In today's post we will see 10 top best practice beginners should keep in mind when writing the java Script.
1) Always write JavaScript in external files
Separating you JavaScript code from your markup is always comes in the good practice. As we maintain our Style-sheet separate from Markup same rule apply for the JavaScript too.It helps you to maintain your JavaScript at one place.
2) Put your JavaScript bottom of the page
Processing of the HTML page always take place from Top to Bottom and placing your heavy files on top will make some delay on the loading so the HTML pages.So, best practice is to place all your JS files at the bottom of the page, to avoid slow loading of the page.
3) Minified your JS
When your JavaScript keep growing you have to keep eye on the size of your JavaScript as it will also hit your performance. So the quick tip is to minified your JavaScript files before moving to server.
4) Always use VAR to declare the variables
Always declare your variables using VAR, otherwise it might be taken as the global variables, which could lead to the logical bug in your program.
5) Use the right script tag
Using <script src="" type="text/script"> is conman practice while writing inline JS. type="text/script" is outdated, so we can drop it easily without worrying about anything.
6) Use unobstrutive JS
Unobstrutive JS is the approach where the practice is to separate the behavior layer from the markup of the webpage.Try to avoid the mixing the JS in the markup.
Example the below given example is not the right approach
<input type="text" onblur="validation()" />
onblur is the JavaScript event and should be handle in the JS file only not in the markup.
7) Use the Literals instead of Objects
When it comes to the performance of the big projects, small things matters a lot. We try in every line to reduce the loading time.One of the important thing is the use of the Literals instead of objects
ex:
instead of using
var obj1 = new object();
var obj1 = {};
var arr = new array;
var arr = [];
and so on. this is FAST, SIMPLE and EASY
8) Eval is bad
We all know very well to avoid Eval while doing JS programming because of the following reasons:
a) Improper use of eval opens up your code for injection attacks
b) Debugging can be more challenging
c) eval'd code executes more slowly
9) Self executing Function
Always use the SEF which makes your code to run as soon the JS is ready.
Syntax:
(function(){
//your code goes here
})();
10) Make use of JSLint
Last but not the least JSLint is very useful when validating your JS and making sure it is as per the standards.
Do login the online JSLint website and play around with the option.
I hope this would have help you in learning.
Comments are welcome.
Happy Coding!!
Neha
No comments:
Post a Comment