3 Common JavaScript Errors and Solutions

Ever since graduating Flatiron School’s immersive software engineering program, I spent time building projects that emphasized JavaScript. I made a Netflix clone during my time in the bootcamp, as well as an Amazon clone and, recently, a LinkedIn clone.

While working on the front end aspects of these projects, I came across a few errors that stumped me in the beginning. As I dived into more research, I found that these are issues many developers come across.

The University of British Columbia actually did a study on JavaScript Errors, studying how these errors come about and what changes these errors can create during the development process. Here is an analysis of what the researchers thought to be the most common JavaScript bugs.

Now, let’s start going through the errors I’ve come across during my coding journey.

  1. “Uncaught TypeError: Cannot Read Property {whatever method you used} of undefined”

I came across this one too many times when I started learning JavaScript, and occurs when there’s an undefined value. For example, let’s say I am creating a variable by declaring “let testing” (which is 100% wrong, there’s more to it). Now let’s try calling any method on it…

Variables must be declared with some value, just “let testing” doesn’t work.

When I created React projects, I came across this type of error by incorrectly using state without properly initializing my state values.

Solution:

Whether you use constructor or the useState hook, the initial state values must be created and set to a default value, like an empty string or an empty array.

2. “Uncaught TypeError: undefined is not a function”

This error came up when I incorrectly called on a function where the value becomes undefined. For example, if we go to the chrome inspector console, let’s try creating a method, “testAlert,” then calling it on the current document.

Solution:

Using arrow functions can solve this error. Look at the following screenshots as guidance.

3. “Uncaught TypeError: Cannot set property”

I come across this error when I incorrectly declare a variable and set it to an undefined value. For example, again in the chrome console.

Once a variable is set to undefined, values can’t be “get” or “set”.

The Fix:

The solution is quite simple since this is a (usually) beginner level error. Declarations must be properly executed.

Resources: