Best Practices: JavaScript, CSS and HTML

Here are the best practices of JavaScript, CSS and HTML.

JavaScript
  1. Choose short and meaningful but readable variable names.

  2. Avoid global variables whenever possible.

  3. Use one global object to encapsulate any global variables you really need.

  4. Always use var to declare your variables.

  5. If you are using any loop like foreach or while with multiple statements then don’t forget to use starting curly brass and ending curly brass. And if you are writing with single statement then avoid starting and ending curly braces.

  6. Try to use comments if necessary which will help you/others to understand better in later days.

  7. Indent your code so it’s readable.

  8. Use curly braces to define blocks of code.

  9. Comment your code.

  10. Always end your statement with a semi colon.

  11. Always write statements in one line because if you split statement in multiple lines then for each line the compiler places a semicolon at the end of a line which is wrong. So you have to more careful while writing.

  12. If possible then try to use Typescript/ JavaScript++ rather JavaScript because it checks syntaxes at compile time and it supports OOPS features.

  13. Maintain two kinds of JavaScript files,

    • Minimized file for production
    • JavaScript file for development.

  14. Be aware of automatic semi-colon insertion happens.

  15. Use foreach rather than for loop.

  16. Maintain JavaScript files light weight, this can be possible for each functionality use different JavaScript file.

  17. Declare variables outside loops.

  18. Reduce DOM operations.

  19. Be aware of duplicate members (member field, member function).

  20. Always place script files reference at the end of the body tag.

  21. Always separate JavaScript code from html file.

  22. Always use === comparison.

  23. Always try to avoid Eval() because it is decreases the performance.

  24. Always try to use {} instead of Object() because Object() decreases the performance.

  25. If possible then use short hand notations.

  26. Use Switch case instead of too many if else blocks.

  27. Use anonymous functions instead of naming functions if possible.

  28. Cache your selectors in variables for further use.

  29. Do not mix CSS properties with JavaScript / JQuery instead we can use CSS classes in JavaScript / JQuery.

  30. Before writing any condition with DOM element, first check whether the DOM element is defined or not.

  31. JavaScript doesn’t validate for duplicate members (member fields for member functions). Be careful while writing JavaScript members.

  32. Move scripts to the bottom (JavaScript)/ Use differ tag when Head only option.

  33. Standardize JavaScript libraries on single framework.

  34. De-reference unused objects.

  35. Reduce Global: Namespace.

  36. Use {} Instead of New Object()

  37. Use [] Instead of New Array()

  38. Use local function variables.

  39. Use === Instead of ==

  40. Remove unnecessary whitespace in scripts.

  41. Never Declare Number, String, or Boolean Objects. Declaring these types as objects, slows down execution speed, and produces nasty side effects.

  42. While development you can place your JavaScript files in your solution because it is easy to test but in production don’t place your JavaScript files inside the solution rather you place the files in other server and give the relative path in your project, make sure that the JavaScript files content cannot be accessed by End-User. That way you can secure your code. For more information, refer the below link:

CSS

  1. Put style sheets at the top (CSS).

  2. Combine multiple cascading style sheets (CSS) into a single style sheet.

  3. If you want to apply CSS class to only one HTML control then go with "#controlID", on the other hand if you want to apply CSS class for multiple controls then go for ".class".

  4. Don't use "style" attribute on an element to set its style.

  5. Use developer tool in all browsers to understand the behaviour of CSS classes which you have applied. Ex. IE developer tool, Fire Bugger etc.

  6. If possible then use bootstrap CSS because it is compatible with mostly with all browsers.

  7. While development you can place your CSS files in your solution because it is easy to test but in production don’t place your CSS file inside the solution rather you place the CSS file in other server at give the relative path in your project, make sure that the CSS files content cannot be accessed by End-User. That way you can secure your code. For more information, refer the below link:

  8. Whenever we test written CSS classes then we should test in all recommended browsers based on your project need. We have to make sure written CSS classes behaves uniquely in all browsers, in other words you can say browser compatibility.

  9. Segregate the CSS files for each module or even for each functionality, try to use light weight which results easier to maintain.

  10. Use sprites for small images.

  11. Keep your code organized with comments.

  12. Use W3C validators, when you use W3C validators then your code will be more accurate and render better.

  13. Use consistent naming conventions.

  14. Use comments as much as possible to increase readability or better to understand at a later point of time.

  15. Use fewer files.

  16. Remove your CSS files from your application solution and place it in one of your secured server and read those files dynamically based on your need. For more details, please follow the below link:

  17. Maintain two kinds of CSS files:

    • LESS files for production
    • CSS file for development

HTML

  1. Writing code according to conventions is important.

  2. Indent your code properly.

  3. Do not forget alt attribute for img tag.

  4. Learn about pair and not pair tags.

  5. Add name attribute for inputs in a form.

  6. Maintain unique names for controls.

  7. Follow proper naming conventions.

  8. Use XHTML rather than HTML because if you use XHTML then it will show you the wrong syntax in HTML page.

  9. If possible use single page applications as it reduces unnecessary round trips.

  10. If possible try to implement functionality at front end as it increases the performance.

  11. Don’t place any inline script or CSS content in HTML page rather use separate files.

  12. If possible try to write unit test methods at client side. Ex. QUnit test etc.

  13. Use common resource file for validation or error messages instead of hard coding HTML labels.

  14. Maintain common resource files for localization or globalization.

  15. To test your code, always use web analyser, developer tool, fiddler, rest console, post master, fire bugger, emulator and simulator etc.

  16. We have to look into speed for the web:

    • Request time
    • Server processing time
    • Data transfer time.
    • Web request time
    • Client processing time

  17. Use the HTTPS2 rather than HTTPS.

  18. Declare your XHTML pages with a DOCTYPE of XHTML1 because XHTML1 is better and faster rendering in the browser.

  19. User the DOCTYPE always.

  20. Use OL, UL, and LI instead of Table if possible.

  21. Use class property for CSS instead of inline CSS for HTML control.

  22. Consider alternatives to style-related tags: <b> or <i> --> <strong>.

  23. Avoid landing page redirects.

  24. Enable compression.

  25. Leverage browser caching.

  26. Minify resources.

  27. Optimize images.

  28. Optimize CSS Delivery.

  29. Remove render-blocking JavaScript.

  30. Use asynchronous scripts.

  31. Avoid plug-ins.

  32. Use legible font sizes.

  33. Use Bootstrap CSS throughout your application as the Bootstrap CSS is already optimized and it supports in all most all browsers and devices irrespective of size of devices and type of devices.

Reference: Refer to the  the below link for a few more best practices.

Up Next
    Ebook Download
    View all
    Learn
    View all