Javascript Code Standards: Difference between revisions
| Line 9: | Line 9: | ||
=== Strings === | === Strings === | ||
Strings should be enclosed | Strings should be enclosed in double quotes ("): | ||
<pre> | <pre> | ||
Revision as of 17:26, 26 October 2022
Naming Conventions
Local Variables
Local variable names should all be lower case:
If the local variable name is made up of multiple words, use snake case:
email_address
Strings
Strings should be enclosed in double quotes ("):
var first_name = "Aaron";
String Concatenation vs. Variable Injection
String concatenation is discouraged. Instead, enclose your strings that require variable injections in backticks (`) and place the variable using ${} notation:
var message = `${user_name} has logged out of the system.`;
Functions
Function names should all be lowercase:
update
If the function name is made up of multiple words, use snake case:
update_by_id
Classes
Class names should all be capitalized:
Vertical
If the class name is made up of multiple words, use Pascal case:
VerticalGroup
Function Definitions
Function definitions should all be full definitions with no shorthand/arrow notation:
function function_name(parameter_1, parameter_2) {
// code goes here
}
In-line Callbacks
In-line callbacks should utilize shorthand/arrow notation:
button.addEventListener('click', (event) => {
event.preventDefault();
});
If the in-line callback is a reusable block of code, create a function for the reusable block and use the function name instead of shorthand/arrow notation:
function validate_form_fields(event, form_fields) {
event.preventDefault();
}
form.addEventListener('submit', validate_form_fields(event, form_fields));