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 look at bad parts of JavaScript that we should avoid.
Global Variables
Global variables are something that we should definitely avoid.
We can use built-in global variables like existing properties of window , Array , String , etc.
However, we shouldn't modify them or add anything to them.
Also, we shouldn't define our own global variables.
People will see things that they don't expect if we add our own members to existing global constructors.
And if we add new global variables, then accidental assignment and naming collisions may result.
We don't want that to happen.
When programs become larger, these will become more of a problem.
We can declare global variables in a few ways. We can use the var keyword at the top level:
var foo = 'bar';or we can create new properties in the window object:
window.foo = 'bar';If we don't use strict mode, we can write:
foo = 'bar';This is supposed to make learning JavaScript easier for beginnings, but forgetting to declare variables is something we should avoid, rather than allowing people to declare variables unintentionally.
Scope
Before the introduction of let and const, JavaScript doesn't have block-scoped variables.
However, now that we have them, we should always use them.
Block-scoped variables data are something that has been missing for a long time.
But now that we have it, we should use them everywhere.
So we can write:
{
let a = 1;
const b = 2;
//...
}or:
if (foo) {
let a = 1;
const b = 2;
//...
}or:
switch (foo) {
case 1: {
let a = 1;
const b = 2;
//...
break;
}
//...
}or:
while (foo) {
let a = 1;
const b = 2;
//...
}and a and b will always only be available within the block.
There's just no excuse not to use them.
Semicolon Insertion
JavaScript will try to add semicolons to places where it thinks it makes sense to do so.
However, we shouldn't depend on this.
That can result in serious errors.
For instance, if we write:
return
{ status: true };in a function, then the function will return undefined .
JavaScript treats this as:
return;
{ status: true };So we should be careful not to let JavaScript add the semicolons for us.
Instead, we write:
return {
status: true
};Reserved Words
There're many reserved words that aren't used in JavaScript. Word like enum , transient, throws are all reserved words but they aren't used in the language for any features.
Reserved words can't be used as variable or parameter names. When reserved words are used for keys in object literals, then they've to be quoted.
They can't be used with the dot notation.
For instance, we have to write:
obj['enum'] = 'foo';or:
const obj = {
'enum': 'foo'
};Unicode
JavaScript characters are 16 bits. This means it's restricted to 65536 characters.
The remaining millions of characters are represented as a pair of characters.
This means that we may have to transform them to their correct form if we need to output them somewhere else.
typeof
The typeof are only useful for primitive values mostly.
For instance, if we write:
typeof 1then we get 'number' .
The problem with typeof comes with testing the types of null and objects.
For instance, typeof null returns 'object' , which isn't correct.
So we can't use typeof to check for null .
Instead, we should use the === operator to check for null :
foo === nullAnother problem with using typeof is when we test for objects.
We can't distinguish different kinds of object or null with it.
For instance:
typeof []and:
typeof {}both return 'object' .
If we have typeof /a/ we may get 'object' or 'function' depending on the browser.
Therefore, typeof isn't very useful for checking null or any other kind of object.
Conclusion
We should avoid JavaScript that is in the latest version but shouldn't be used.
This includes declaring global variables and augmenting existing global variables.
Also, we should stick with a block-scoped variable that is confined to a block.
This removes lots of confusion with variables that are created before they existed.
The typeof operator also isn't very useful for checking types of objects.
In Plain English
Show some love by subscribing to our YouTube channel!