JavaScript Coding Standard Guidelines
Let’s check out the format and guidelines that you need to follow while writing a coding article at w3wiki. The programming articles should contain the following points:
Variable Declaration with const and let
For ES2015 or newer versions, let and const should be used in place of var. A declaration always uses const if its value is not changed, otherwise, use let.
// Value can be changed
let myVar = 'w3wiki';
// Value cannot be changed
const myVar2 = 'GfG';
Naming Conventions
Use the full words for Variable and function names, and use camel case with a lowercase first letter. Names should be descriptive, but not excessive.
let myGeek;
const myGeek;
Note: Exceptions are allowed for iterators, such as the use of i to represent the index in a loop.
Code width
Fix the character limit per line (Max 60 Characters including space).
Semicolons
Always use semicolons at the end of statements.
let myGeek = 'w3wiki';
Spacing
- Indentation with Tabs.
- No whitespace at the end of the line or on blank lines.
- Unary special-character operators (e.g., ++, –) must not have space next to their operand.
- Any , and ; must not have preceding space.
- Any ; used as a statement terminator must be at the end of the line.
- Any : after a property name in an object definition must not have a preceding space.
- The ? and : in a ternary conditional must have space on both sides.
- No filler spaces in empty constructs (e.g., {}, [], fn()).
- All function bodies are indented by one tab, even if the entire file is wrapped in a closure.
Comments
Always put the comments before the code to which they refer, and always use preceded blank lines. Always use a single space between comments and tokens (//) and use the first capital letter.
// Declaring a variable with a string value
let myGeek = 'w3wiki'
Blocks and Curly Braces
For if/else/for/while/try blocks, always use braces and go to the next line.
If ( x === 4 ) {
console.log(x);
} else {
console.log("error");
}
Multi-line Statements
If the given statements are very long, then use a line break after an operator.
let expr = ‘<p>Sum of two numbers ’ + x + ‘ and ‘ + y +
‘ is ‘ + x + y + ‘</p>’;
Line break for Conditional & Logical Operators
If conditional statements are too long, then we can break the statements after operators into multiple lines.
let num1 = 10;
let num2 = 20;
let num3 = 30;
if (
var1 == var2 &&
var2 == var3 &&
var3 == 50
) {
console.log(var1)
}
Chained Method Calls
If long-chained methods are used, then use one method call per line.
Javascript
const object = [ { name: "Baleno" , type: "car" }, { name: "Apple" , type: "fruit" }, { name: "Chocolate" , type: "sweet" }, { name: "onion" , type: "vegetable" } ]; // Chaining methods object .map(item => item.type) .reduce((result, fruit) => { result.push(fruit); return [... new Set(result)]; }, []); |
Strings Declaration
Use single quotes to define string literals.
const str = ‘w3wiki’
Use the escape character with a backslash to add single quotes in the string.
let str = ‘Welcome to \’w3wiki\’’
Array Declaration
For array declaration, please prefer to use [] constructor to create an array rather than Array() constructor.
const arr = [];
// Initializing the array like that:
const arr = [ 1, 2, 3, 5 ];
How to Write JavaScript Articles on w3wiki ?
w3wiki allows coding lovers to display their programming skills by writing Javascript-based articles. However, a lot of individuals (especially college students or beginners) find it challenging to describe their learnings and talents and contribute to w3wiki. But now the problem has been solved. This article will guide you through the entire process and guidelines for writing articles at w3wiki.
Contact Us