JavaScript Coding Conventions
Sources:
- **
Overview
Object Name | Notation | Length | Plural | Prefix | Suffix | Abbreviation | Char Mask | Underscores |
---|---|---|---|---|---|---|---|---|
Function name | camelCase | 50 | Yes | No | Yes | Yes | [A-z][0-9] | No |
Function arguments | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Local variables | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Constants name | PascalCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Field name | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Coding conventions are style guidelines for programming. They typically cover:
- Naming and declaration rules for variables and functions.
- Rules for the use of white space, indentation, and comments.
Coding conventions secure quality:
- Improves code readability
- Make code maintenance easier
Always use the same naming convention for all your code. For example:
- Do use camelCasing for variables, function names and function argument names;
- Do use PascalCasing for global variables;
- Do use UPPERCASE for constants (like PI);
- Do not use under_scores in variable, constants, function arguments or function names;
- Do not use hyphens in JavaScript names.
Naming Conventions
Do use camelCasing for function names:
Do use camelCasing for function arguments and local variables:
Note: Don’t start names with a $ sign. It will put you in conflict with many JavaScript library names.
Spaces Around Operators
Always put spaces around operators ( = + / * ), and after commas:
Examples:
Code Indentation
Always use 4 spaces for indentation of code blocks:
Functions:
Note: Do not use tabs (tabulators) for indentation. Text editors interpret tabs differently.
Statement Rules
General rules for simple statements: Always end simple statement with a semicolon.
Examples:
General rules for complex (compound) statements:
- Put the opening bracket at the end of the first line.
- Use one space before the opening bracket.
- Put the closing bracket on a new line, without leading spaces.
- Do not end complex statement with a semicolon.
Functions:
Loops:
Conditionals:
Object Rules
General rules for object definitions:
- Place the opening bracket on the same line as the object name.
- Use colon plus one space between each property and its value.
- Use quotes around string values, not around numeric values.
- Do not add a comma after the last property-value pair.
- Place the closing bracket, on a new line, without leading spaces.
- Always end an object definition with a semicolon.
Example:
Short objects can be written compressed, on one line, like this:
Line Length < 80
For readability, avoid lines longer than 80 characters. If a JavaScript statement does not fit on one line, the best place to break it, is after an operator or a comma.
Example:
Loading JavaScript in HTML
Use simple syntax for loading external scripts (the type attribute is not necessary):
Accessing HTML Elements
A consequence of using “untidy” HTML styles, might result in JavaScript errors. These two JavaScript statements will produce different results:
If possible, use it naming convention (as JavaScript) in HTML.
File Extensions
- HTML files should have a .html extension (not .htm);
- CSS files should have a .css extension;
- JavaScript files should have a .js extension.
Official Reference
Resources
Appendix
Note created on 2024-05-09 and last modified on 2024-05-09.
Backlinks
(c) No Clocks, LLC | 2024