JavaScript coding standards/conventions
The long-term value of software is depended of the quality of codebase and it helps to reducing the weakness of programs. That’s why we should follow standards while writing Js code which will increase the code quality. I am trying here to give you some idea/suggestion to write better Js code.
Below are some suggestions which should follow everyone:
- Try to avoid embedded JavaScript code in HTML files. It will decrease page loading time and increase caching and compression opportunity.
- Avoid longer line. Break the line when a statement will not fit on a single line.
- All JavaScript programs should be stored in a separate Js file and should be placed as late in the body as possible. This reduces the effects of delays.
- Indenting should be followed constantly. Now a day’s space is not a matter because you can eliminate by using minification.
- Function name should start with small letter and all meaningful word should be capital letter.
- Should not use space between the name of a function and left parenthesis and separate parameters by a space.
function showFlashMessage(message, message_type){
} - Global variable should be all caps, local Variable in small letter and all meaningful word should be separate by underscore (_)
- All variables should be declared before using though JavaScript does not require this. The variable declaration statement should be the first statement of a function. It will make your code easier to read.
- Must be use semicolon (;) at the end of the every statement and each line should contain only a statement.
- Use left curly braces in the same line of the function or a statement and a line break for curly braces.
for(){
if(condition){
statements;
}
} - Try to avoid comma operator except for disciplined use.
Some related posts:
I strongly recommend to read 24 JavaScript Best Practices for Beginners
Nice post for beginners, actually it might hello all because it is summarize list..