JavaScript is an easy to learn programming language. It’s easy to write programs that run and does something. However, it’s hard to account for all the uses cases and write robust JavaScript code.
In this article, we’ll look at how to properly space out the JavaScript code.
Use Indentation for Making Long Method Chains
If we have method chains, then we should indent the method calls so that we see the chain more easily.
For example, instead of writing:
$('#foo').find('.selected').highlight().end().find('.bar').count;
We instead write:
$('#foo')
.find('.selected')
.highlight()
.end()
.find('.bar')
.count;
Leave a Blank Line After Blocks and Before the Next Statements
If we have blocks followed by other statements, then we should follow that with an empty line so that we have the next statement separated from the block.
For instance, instead of writing:
if (foo) {
return foo;
}
return baz;
We write:
if (foo) {
return foo;
}
return baz;
It’s much clearer to separate them with an empty line.
Don’t Pad Blocks with Blank Lines
We shouldn’t pad blocks with empty lines.
This is because they don’t help with readability and takes up space.
For instance, instead of writing:
function foo() {
console.log('bar');
}
We write:
function bar() {
console.log(foo);
}
Don’t Use Multiple Blank Lines
We shouldn’t use multiple blank lines since they take up space and don’t help with improving readability.
For example, instead of writing:
const fullName = 'joe';
const email = 'joe@joe.com';
We write:
const fullName = 'joe';
const email = 'joe@joe.com';
Don’t Add Spaces Inside Parentheses
Spaces inside parentheses aren’t needed.
For instance, instead of writing:
function baz( foo ) {
return foo;
}
We write:
function baz(foo) {
return foo;
}
Don’t Add Spaces Inside Brackets
Likewise, we shouldn’t add spaces inside parentheses.
For instance, instead of writing:
const foo = [ 1, 2, 3 ];
We write:
const foo = [1, 2, 3];
If we access array entries or object properties, instead of writing:
foo[ 1 ]
We write:
foo[1]
And instead of writing:
bar[ 'baz' ]
We write:
bar['baz']
Add Spaces Inside Curly Braces
We should have spaces inside curly braces to improve readability.
For instance, instead of writing:
const foo = {baz: 1};
We should write:
const foo = { baz: 1 };
With the extra spaces, the code is much easier to read.
Avoid Lines of Code that are Longer Than 100 Characters
Lines of code that are longer than 100 characters may overflow people’s screens.
If that happens, they need to scroll horizontally to read the whole line.
To avoid that, we should our lines of code to 100 characters or less.
Consistent Spacing Inside an Open Block and Next Token on the Same Line
We should have consistent spacing between the open block and the next token.
For instance, instead of writing:
function foo() {return 'bar';}
We should write:
function foo() {
return 'bar';
}
The extra spaces make our function easier to read.
Avoid Spaces Before Comma and Require a Space After Commas
We should have spaces before a comma and have one space after a comma.
For instance, instead of writing:
const arr = [1 , 2];
We should write:
const arr = [1, 2];
It’s much more readable.
Enforce Spacing Inside a Computed Property Bracket
Spacing should be enforced inside a computed property bracket.
For instance, instead of writing:
obj[foo ]
We write:
obj[foo]
No Spaces Between Function and its Invocation
We don’t need a space between a function and its invocation.
For example, instead of writing:
foo ();
or:
foo
()
We write:
foo();
Having Spacing Between Keys and Values in Object Literal Properties
We should have some spaces between keys and values in object literal properties.
For example, instead of writing:
const obj = {foo:1};
We should write:
const obj = {
foo: 1
};
No Trailing Spaces at the End of Lines
A trailing space at the end of a line is useless.
Therefore, we should remove them if they are there.
We should also configure our text editor to remove them automatically.
Conclusion
We should have spacing in places where they’re needed like in keys-value pairs of objects.
However, we shouldn’t have spaces where they are needed like the end of lines, extra blank lines, and other things like that.