-
Notifications
You must be signed in to change notification settings - Fork 11
Formatting
Use 2 spaces for indenting your code and swear an oath to never mix tabs and spaces
According to scientific research, the usage of semicolons is a core value of our community. Consider the points of the opposition, but be a traditionalist when it comes to abusing error correction mechanisms for cheap syntactic pleasures.
Limit your lines to 80 characters. Yes, screens have gotten much bigger over the last few years, but your brain has not. Use the additional room for split screen, your editor supports that, right?
Use single quotes, unless you are writing JSON. This helps you separate your objects’ strings from normal strings.
Right:
var foo = ‘bar’;
Wrong:
var foo = “bar”;
Your opening braces go on the same line as the statement.
Right:
if (true) {
console.log(‘winning’);
}
Wrong:
if (true)
{
console.log(‘losing’);
}
Also, notice the use of white space before and after the condition statement. What if you want to write ‘else’ or ‘else if’ along with your ‘if’…
Right:
if (true) {
console.log(‘winning’);
} else if (false) {
console.log(‘this is good’);
} else {
console.log(‘finally’);
}
Wrong:
if (true)
{
console.log(‘losing’);
}
else if (false)
{
console.log(‘this is bad’);
}
else
{
console.log(‘not good’);
}
Declare one variable per var statement, it makes it easier to re-order the lines.
Right:
var keys = [‘foo’, ‘bar’];
var values = [23, 42];
var object = {};
Wrong:
var keys = [‘foo’, ‘bar’],
values = [23, 42],
object = {},
key;
- 20210909