Categories
JavaScript Best Practices

JavaScript Best Practices — Avoiding Bad and Outdated Features

Spread the love

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 the best way to define and work with functions.

Empty Catch Blocks

Empty catch blocks don’t anything. At least we should justify why the block is blank with a comment.

So we shouldn’t just leave it blank.

Switch Statements Fall-Throughs

switch blocks usually shouldn’t have fall-throughs. If we add them, we should say why we have them so that no one will be confused that it’s a mistake.

For instance, we should write:

switch (val) {  
  case 1:  
  case 2:  
    foo();  
    // fall through  
  case 3:  
    bar();  
    break;  
  default:  
    baz(val);  
}

default Case

We should have the default case.

This way, any value that doesn’t match any cases is processed.

The default statement must be last so that it’s only run when nothing else matches.

this

this should only be used in constructors and methods.

Arrow functions shouldn’t have their own this .

this should never refer to the global object.

Equality Checks

We should always use === or !== for equality and inequality comparisons respectively.

Otherwise, we may get data type coercion that we don’t want before checking comparison.

Exceptions Where Coercion is Desirable

We may want to use == for compare both null and undefined at the same time.

For instance, we can write:

if (value == null) {  
  //...  
}

with

We should never use the with keyword. It creates a new scope and causes confusion when we define a variable within in a with block.

It’s also not allowed in ES5 strict mode.

Dynamic Code Evaluation

We should never use eval or the Function constructor to run code or create new functions respectively.

Automatic Semicolon Insertion

Semicolons should be added by programmers rather than the JavaScript interpreter.

If we let the interpreter add it, then it may be in unexpected places.

Non-Standard Features

Features that aren’t in stage 4 in the W3C specification shouldn’t be used.

Also, we’ve to take into account what is supported by browsers before using our code.

Old features that have been removed like WeakMap.clear shouldn’t be used.

Wrapper Objects for Primitive Types

Never use new with primitive object wrappers such as Boolean, Number, String, Symbol .

We should use them without new .

This way, they return primitive values instead of objects.

For instance, we write:

const foo = `Boolean(1);`

instead of:

const foo = new `Boolean(1);`

Modifying Built-in Objects

Built-in objects should never be modified.

Objects like Array or String prototypes shouldn’t have new methods added to it.

We also shouldn’t modify methods in those objects.

Also, don’t add symbols to the global object.

If we want extra functionality, we should write our own code or use a standard polyfill.

Omitting () When Invoking a Constructor

We should always add parentheses when invoking a constructor.

For instance, we should write:

new Foo();

instead of:

new Foo;

Even though it’s allowed, we shouldn’t do it.

Conclusion

Just because some features are in JavaScript doesn’t mean we should use it.

Outdated features like eval and the Function constructor shouldn’t be used.

Wrapper objects shouldn’t be used to create primitive values.

Also, we should have a comment in empty catch blocks and switch case fallthroughs.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *