Top 10 JavaScript Errors From 1000+ Projects (and How to Avoid Them)

We look at the most common errors made while developing in JavaScript by browser and what you as a web dev can do to avoid them.

To give back to our community of developers, we looked at our database of thousands of projects and found the top 10 errors in JavaScript. We’re going to show you what causes them and how to prevent them from happening. If you avoid these “gotchas,” it’ll make you a better developer. computer science course computer science jobs computer science binus

Because data is king, we collected, analyzed, and ranked the top 10 JavaScript errors. Rollbar collects all the errors for each project and summarizes how many times each one occurred. We do this by grouping errors according to their fingerprints. Basically, we group two errors if the second one is just a repeat of the first. This gives users a nice overview instead of an overwhelming big dump like you’d see in a log file.  computer science course computer science jobs computer science binus

We focused on the errors most likely to affect you and your users. To do this, we ranked errors by the number of projects experiencing them across different companies. If we looked only at the total number of times each error occurred, then high-volume customers could overwhelm the data set with errors that are not relevant to most readers.  computer science course computer science jobs computer science binus

Here Are the Top 10 JavaScript Errors:

Each error has been shortened for easier readability. Let’s dive deeper into each one to determine what can cause it and how you can avoid creating it.  computer science course computer science jobs computer science binus

1. Uncaught TypeError: Cannot Read Property

If you’re a JavaScript developer, you’ve probably seen this error more than you care to admit. This one occurs in Chrome when you read a property or call a method on an undefined object. You can test this very easily in the Chrome Developer Console.  computer science course computer science jobs computer science binus

This can occur for many reasons, but a common one is the improper initialization of state while rendering the UI components. Let’s look at an example of how this can occur in a real-world app. We’ll pick React, but the same principles of improper initialization also apply to Angular, Vue or any other framework.  computer science course computer science jobs computer science binus

class Quiz extends Component {
  componentWillMount() {
    axios.get('/thedata').then(res => {
      this.setState({items: res.data});
    });
  }

  render() {
    return (
      <ul>
        {this.state.items.map(item =>
          <li key={item.id}>{item.name}</li>
        )}
      </ul>
    );
  }
}

There are two important things realize here:  computer science course computer science jobs computer science binus

  1. A component’s state (e.g. this.state) begins life as undefined computer science course computer science jobs computer science binus
  2. When you fetch data asynchronously, the component will render at least once before the data is loaded – regardless of whether it’s fetched in the constructor, componentWillMount or componentDidMount. When Quiz first renders, this.state.items is undefined. This, in turn, means ItemList gets items as undefined, and you get an error – “Uncaught TypeError: Cannot read property ‘map’ of undefined” in the console.  computer science course computer science jobs computer science binus

This is easy to fix. The simplest way: Initialize state with reasonable default values in the constructor.  computer science course computer science jobs computer science binus

Prev1 of 7Next

Leave a Reply

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