Chapters

JavaScript Coding Standards

Semicolons

Use them. Never rely on Automatic Semicolon Insertion (ASI).

Blocks and Curly Braces

if, else, for and while blocks should always use braces, and always go on multiple lines. The opening brace should be on the same line as the function definition, the conditional, or the loop. The closing brace should be on the line directly following the last statement of the block.

        
            let a, b, c;

if ( myFunction() ) {
    // Expressions
} else if ( ( a && b ) || c ) {
    // Expressions
} else {
    // Expressions
}        
    

JavaScript ES6+

As much as possible, we strive to use JavaScript ES6  (at the time of writing). For example, this means using:

  • const and let over var.
  • Arrow functions: const myFunction = () => {}

Components

For readability and maintainability, it is important to use components. There are examples of this in our boilerplate, but this essentially means using export and import. For example:

myFunction.js:

        
            export default function myFunction() {
    ...
}        
    

main.js:

        
            import myFunction from './myFunction';

// Load all functions after page is loaded
window.addEventListener("DOMContentLoaded", () => {
    myFunction();
    // Your other functions here
});