JavaScript is a very forgiving language. It’s easy to write code that runs but has mistakes in it.
In this article, we’ll look at some useless code that we don’t need to put into our JavaScript code.
No Extra Parentheses
We don’t need parentheses around expressions like arithmetic expressions unless we want to group things that have lower precedence together.
Therefore, we don’t need parentheses on expressions like:
(a * b);
or:
(a * b) + c;
Also, we don’t need parentheses around things like loop variables:
for (let a of (b)){
//,,,
}
We can remove the parentheses around the b
in the loop.
Some operator expressions also don’t need brackets like:
typeof (foo);
We don’t need to wrap parentheses around foo
.
Below is another example that has extra parentheses:
(function(){} ? foo() : bar());
We can remove parentheses at the beginning and the end of the line.
If we choose to do an assignment inside a conditional, then we should put parentheses around them to make it clear which variable we’re assigning values to:
while ((foo = bar())) {}
The exception would be a normal for
loop, which we write without parentheses:
for (let i = 0; i < 10; i++) {
//...
}
Like with the assignment within loops, we should also put parentheses around assignment expressions in return
statements for clarity:
const foo =(b) => {
return (b = 1);
}
Remove Extra Semicolons
JavaScript lets us put as many semicolons at the end of the line as we want. It does the same thing as a single semicolon, so the extra semicolons are useless.
We also don’t need semicolons after function declarations.
For instance, we shouldn’t have multiple semicolons in the following line:
let x = 1;;;
Instead, we should write:
let x = 1;
An example of a function declaration is the following code:
function foo() {
};
We don’t need an extra semicolon in the code above. Instead, we should write:
function foo() {
}
Reassigning Function Declarations to a Different Value
Since JavaScript functions are just regular variables, they can also be reassigned to anything.
It’s often an indication of a mistake since we would probably want to call the function rather than assigning it to something else.
For example, we don’t want to write:
function fn() {}
fn = foo;
Instead, we should create a variable explicitly and assign a function to the variable to make it clear that it’s actually a variable that we want to reassign values to:
const fn = `function() {}
fn = foo;`
Photo by Christopher Carson on Unsplash
Assigning Members Imported from Modules to Another Value
Since imported members of JavaScript modules are read-only, we can’t assign them to a new value. Therefore, we should have code like:
import { x, y } from "./foo";
x = 1;
We’ll get the error that x
is read-only.
This also applies to the importing of default exports.
For instance:
import x from "./foo";
x = 1;
also give the same error.
The following doesn’t give an error with some build tools:
import * as foo from "./foo";
foo.x = 1;
Function Declarations in Nested Blocks
Before ES6, function declarations in nested blocks are supposed to be in the top level of a program or a body of another function.
However, some JavaScript interpreters let function declarations be added to nested blocks.
For example:
function foo() {
if (true) {
function bar() {}
}
};
isn’t supposed to be valid JavaScript because we have the bar
function declaration in the if
block of the foo
function.
To make our code syntactically correct, we should remove the bar
function declaration from the if
block. We can also use function expressions within our code blocks if we do want to define a function inside.
Invalid Regular Expressions in RegExp Constructors
We don’t want invalid regular expressions in RegExp
constructors. The constructor takes any string and tries to return a regular expression object out of it.
Invalid regular expression definitions include things like:
RegExp(']')
RegExp('-', 'z')
We should make sure we pass in a valid regex string or use regex literals instead.
Conclusion
There’re lots of things that JavaScript interpreters allow, but they’re either useless or causes errors. For example, useless parentheses and semicolons should be removed. Function declarations also shouldn’t be reassigned to new values.
Function declarations also aren’t allowed in blocks, even though JavaScript interpreters may still run it.
Finally, invalid regex strings shouldn’t be passed into the RegExp
constructor.