JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.
In this article, we’ll take the good parts of JavaScript code styling for formatting, and look at other good features of JavaScript.
Code Styling
We should avoid features that are occasionally hazardous but are useful.
If we do that, we can avoid a large class of errors.
If we communicate clearly with our code, then it’s less likely to break in the future.
Blocks
Consistent styles should be used so that we can understand them easily.
For instance, we should make indentations and put each statement on their own line.
We should always use blocks to surround any statements.
For instance, instead of writing:
if (a)
b( );
We write:
if (a){
b( );
}
If we have:
if (a)
b( );
c( );
Then it looks like:
if (a) {
b( );
c( );
}
But it means:
if (a) {
b( );
}
c( );
Therefore, we should use curly braces to surround blocks even if we don’t have to.
JavaScript have block-scoped variables, so we should declare them before they’re first used.
Things That Look Like Mistakes
Other things that look like mistakes include using assignment in if
statement:
if (a = b) { ... }
We probably want to write:
if (a === b) { ... }
Switch Statements
We should never forget to add a break
to the end of each case
in a switch
statement.
Letting them fall through is just a bad idea.
Sometimes we may want to do that intentionally, but most of the time, we probably don’t.
So we should always write:
switch (val) {
case 1: {
//...
break;
}
case 2: {
//...
break;
}
//..
}
Global Variables
Global variables should be avoided as much as possible.
We can use the existing ones that are built into the JavaScript standard library.
However, we shouldn’t define new ones or override the existing ones by replacing or adding new methods to them.
If we declare variables in scripts, we can declare them in blocks or closures.
Also, we can consider using modules to make variables private.
Beautiful Features
There’re some great features to simplify writing programs in JavaScript.
They include functions being first-class objects. They can be passed in as arguments to other functions and be assigned to variables.
Also, they can have their own properties.
This allows them to be used as callbacks.
Dynamic objects can be used to our benefit.
We can define one-off objects we object literals. Therefore we can use them as dictionaries.
Objects can inherit members from other objects directly.
Likewise, array literals are also very handy for defining arrays.
We can just define arrays without using any classes or constructors.
Having pi its own constant is also a great feature.
In JavaScript, a word can’t be used as a reserved word, a parameter, or a function name at the same time.
This makes our code easier to understand since they can’t have multiple meanings.
With let
and const
, we have block-scoped variables. This way, they’re always confined to the block.
All these features allow us to make our code simple, which is good.
Conclusion
There are many good parts of JavaScript.
Therefore, we should use them as much as possible.
They include object and array literals, block-scoped variables, indentation, and curly braces for blocks.
Using functions like any other object is another great feature that we should take advantage of.