Indentation rules
-
Rule: Follow the indentation rule of 2 spaces for JavaScript.
-
Reason: Consistent indentation improves readability and helps identify the structure of the code. 2-space indentation is commonly adopted in JavaScript projects.
Example (Correct):
if (true) {
console.log('Hello, world!');
}
Example (Incorrect):
if (true) {
console.log('Hello, world!');
}
Braces on the Same Line (1TBS)
-
Rule: Place braces on the same line as their corresponding statements, using the 1TBS (One True Brace Style).
-
Reason: The 1TBS style improves code compactness and consistency, making it easier to read and follow.
Example (Correct):
if (condition) {
console.log('True');
}
Example (Incorrect):
if (condition)
{
console.log('True');
}
Use Single Quotes for Strings
-
Rule: Use single quotes for strings unless you are writing JSON.
-
Reason: Single quotes are the preferred style in JavaScript unless double quotes are required (e.g., for JSON or when a single quote appears in a string).
Example (Correct):
const greeting = 'Hello, world!';
Example (Incorrect):
const greeting = "Hello, world!";
Summary:
-
Code formatting: 2 spaces for indentation, braces on the same line (1TBS), single quotes for strings.