Introduction
Here are the best practices of JavaScript, CSS, and HTML.
JavaScript
- Choose short and meaningful but readable variable names.
- Avoid global variables whenever possible.
- Use one global object to encapsulate any global variables you really need.
- Always use var to declare your variables.
- 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 a single statement then avoid starting and ending curly braces.
- Try to use comments if necessary which will help you/others to understand better in later days.
- Indent your code so it’s readable.
- Use curly braces to define blocks of code.
- Comment your code.
- Always end your statement with a semicolon.
- 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.
- If possible then try to use Typescript/ JavaScript++ rather JavaScript because it checks syntaxes at compile time and it supports OOPS features.
- Maintain two kinds of JavaScript files,
- Minimized file for production
- JavaScript file for development.
- Be aware of automatic semi-colon insertion happens.
- Use foreach rather than for loop.
- Maintain JavaScript files lightweight, this can be possible for each functionality use different JavaScript file.
- Declare variables outside loops.
- Reduce DOM operations.
- Be aware of duplicate members (member field, member function).
- Always place script files reference at the end of the body tag.
- Always separate JavaScript code from the HTML file.
- Always use === comparison.
- Always try to avoid Eval() because it decreases the performance.
- Always try to use {} instead of Object() because Object() decreases the performance.
- If possible then use shorthand notations.
- Use the Switch case instead of too many if-else blocks.
- Use anonymous functions instead of naming functions if possible.
- Cache your selectors in variables for further use.
- Do not mix CSS properties with JavaScript / JQuery instead we can use CSS classes in JavaScript / JQuery.
- Before writing any condition with the DOM element, first, check whether the DOM element is defined or not.
- JavaScript doesn’t validate for duplicate members (member fields for member functions). Be careful while writing JavaScript members.
- Move scripts to the bottom (JavaScript)/ Use different tags when the Head only option.
- Standardize JavaScript libraries on a single framework.
- De-reference unused objects.
- Reduce Global: Namespace.
- Use {} Instead of New Object()
- Use [] Instead of New Array()
- Use local function variables.
- Use === Instead of ==
- Remove unnecessary whitespace in scripts.
- Never Declare Number, String, or Boolean Objects. Declaring these types as objects slow down execution speed, and produces nasty side effects.
- 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:

Nilesh ShahPosted Jan 6, 2020, 8:00 AM
I suggest, rather than providing only bullet points in this 1 article, you can elaborate all points with examples and make this a multi-part article series.
Kuppurasu NagarajPosted May 17, 2016, 11:19 AM
Nice sharing..
Raja TPosted May 17, 2016, 3:02 AM
Nice, Thanks for sharing
Ravi PatelPosted May 17, 2016, 1:02 AM
nice share