---
description: Learn what different error messages mean, as well as how to read and fix code
---

# Reading Code & Fixing Errors

## **Objective**

## Lecture&#x20;

**Strategy Guide**\
**How to fix errors**

## **Overview**

**Fixing errors is a large part of software development. In fact, some development jobs are just about fixing errors . This guide is meant to assist you thought process of fixing errors, as well as expose you to common errors.  Solving errors is arguable the most important part of coding.**

## **Error Rules**

1. **DON’T GET FRUSTRATED. Errors are good. Yes, errors are good. They communicate what is wrong with our code. The worst case is when your code doesn’t work how you expect and you are not getting any errors. Be glad you have an error.**
2. **SEE RULE #1.**
3. **Start at the top of the error message. Sometimes your error message can be massive and overwhelming. Always start at the top because that first error could be a syntax error causing all the other errors.**
4. **Read the error message.  Generally it will tell us what type of error we have and its location. After you read the message think what, how, and why is this error message coming up.  Why would this variable be undefined? What code did I just change? How is this type of error thrown?**
5. **Lastly, google the error message.  When searching for the error remove any specific variables to your  case. For example in the image below remove names and search what is a reference error is not defined**

## **A Common Error Message**

![Screen Shot 2017-08-28 at 3.31.10 PM.png](https://lh3.googleusercontent.com/iXxDGQtvqbkUiksAqQNACkdWEYTj4Ba9CNaEI3lejJ1l8bzFB5hGXjTKx2mQ4SG_yLWRnhK7KwSnDCnM9UY5pPr_I72BTeM_SOE2_j4AceAboTOdSTGrSRfG8tLcCunqAHFwnA)

**Anatomy of the Error Message**\
![Error Example.jpg](https://lh3.googleusercontent.com/CExHfUj7qDbRODiyJebV9Fs-Ae5zrbfLtJpw1xIC4q5ly9OquTfm1pM70fra96Kcs-psOJC4NzQmeFF3BYddqLgR5oIwA5AA9kssTF2H2h9naWb8jcFbbVFwU5j-RDPi_WXF0A)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

**In the above example, in the GREEN the Error Message is telling us we have a ReferenceError names is not defined. This line is what we would google. In this case names does not have a value when I am trying to use it.** \
**The location of the error is in YELLOW.  home/unbuntu/workspace/fix-errors/lesson2.js:2:22.   So the error is in the fix-errors folder and in the lesson2.js. The first number 2 is the line on the file and second number 22, is for character 22.**

## **Error Types**

### **Syntax Error**

**Syntax or format errors are a major problem for most beginning programmers.  A syntax error occurs when the programmer fails to obey one of the grammar rules of the language.  Typically this involves things like using the wrong case, putting punctuation where it’s not supposed to be or failing to put punctuation where it is supposed to be, etc.**

&#x20;**Common Causes Include:**

* **Javascript:  Unclosed or not matching curly braces, brackets, quotes and parentheses**  \
  **{  },   \[ ], “ ” ,    ( )**
* **HTML   Unclosed or not matching element tags,   <> \</>** &#x20;
* **A simple typo in the middle of the code.**

### **ReferenceError**

**Reference Errors simply means the program cannot access the variable.**&#x20;

**Common Causes Include**

* **Variable has not been created at that moment when the code reaches it.**
* **Variable is out of scope**

### **Type Errors**

**Type Errors occur when we try to access properties or methods that do not exist on a data type.** \
**An extremely common type error is “Cannot read property 'length' of undefined”.  This error does not mean length is undefined, but rather that wherever we tried to use the .length property on a variable or data before it was defined.  Undefined does not have any properties so trying to use .length or any property for that matter on undefined throws an error.**

**Common Causes Include**

* **Variable is not defined or does not have a value yet**
* **Trying to access a property or method that does not exist on this data type**

## **Strategies to Fix Errors**

* **If you are having a hard time of locating the exact location you can comment out blocks of your code until it runs. Then slowly uncomment out blocks of code. This is more useful when you have large files with lots of code.**
* **Confirm what you believe with console.logs. Never assume your code works always check the flow of data and variables values with console.logs.**&#x20;

## **Prevention**

**We will never be able to fully prevent errors when we code, besides that would make coding too easy. It is very important to work slowly and confirm a function or block of code is working how you suspect before moving onto another part of the problem. Simple baby steps is the best way to prevent code errors.. We want to confirm each step mentally before moving on.  It is also easier to fix your errors when you move slowly and double check your work. If you assume your function or block of code works without testing it and then you begin to write new code that uses that function or code, it can become quite difficult to track down the real errors. We want limit the amount of error possibilities to check and this can be prevented by just double checking your work before moving on.**\


## Q**uiz: Errors**

{% embed url="https://docs.google.com/forms/d/e/1FAIpQLScJfAXEcKiDXQ1Npu2tuV7iw31ctlzjuaxBWLTtSRXSzltrrQ/viewform" %}

## Challenge #2

{% embed url="https://docs.google.com/document/d/1gnnWImIt8LM4lL4nY-6XQ7UEt5Fs6GcT1Jjwb01K8sg/edit#heading=h.kk1966kbedef" %}

1. **Go over new code and be able to parse it**
2. **Review previous concepts and learn new ones**
3. **Learn how to find answers and research**

## **Additional Resources**

**These resources are very helpful when you are trying to solve a problem or need to look up documentation.**

1. [**Mozilla Developer Network (MDN)**](https://developer.mozilla.org/en-US/)
2. [**DevDocs.io**](https://devdocs.io/)
3. [**Stack Overflow**](https://stackoverflow.com/)

## **Overview**

**Fixing errors is a large part of software development. In fact, some development jobs are just about fixing errors . This guide is meant to assist your thought process of fixing errors, as well as expose you to common errors.  Solving errors is arguably the most important part of coding.**

## **Error Rules**

1. **DON’T GET FRUSTRATED. Errors are good. Yes, errors are good. They communicate what is wrong with our code. The worst case is when your code doesn’t work how you expect and you are not getting any errors. Be glad you have an error.**
2. **SEE RULE #1.**
3. **Start at the top of the error message. Sometimes your error message can be massive and overwhelming. Always start at the top because that first error can be a syntax error causing all the other errors.**
4. **Read the error message.  Generally it will tell us what type of error we have and its location. After you read the message think what, how, and why is this error message coming up.  Why would this variable be undefined? What code did I just change? How is this type of error thrown?**
5. **Lastly, google the error message.  The the Error Message Type.**

## **A Common Error Message**

![Screen Shot 2017-08-28 at 3.31.10 PM.png](https://lh5.googleusercontent.com/OhW5Rx9n32nzLw6BPOHug3rNMcD_UCg2Y6kAbULla2GwDhWFstZ_bInLgOzD1Xnj8-_BwKv4xIGaevRSDlCyre4F3ifSPY1EMy6ry6l0haJxu6A9Mjfhzgtj1KcnLpKlhEc4i9y7)

**Anatomy of the Error Message**\



![Error Example.jpg](https://lh4.googleusercontent.com/r7nEtExPqTJLlojGgQl1VAkvqBVhHrwYkjPJsSG7rG50iD20N8hBKveT_JBtjlUMv3zf081BKSLzl4MOi8E0WNnDUR1eoNgX31EPnTf2JAH1kahhy-fwR3VeBc1wAzuPwICvZi76)

**In the above example, in the GREEN is the Error Message. The error message is communicating to us we have a:**\
**ReferenceError names is not defined.**\
&#x20;**This line of our error message is what we would google but we would leave names out of the search, as that is a specific variable to our problem and we want to search for general problems and solutions. The important part is the type of error, which is listed first, in this case a ReferenceError.  Other types of errors are Syntax Errors, Type Errors and Runtime Errors.**\
\
**The location of the error in the image above is in YELLOW.  home/unbuntu/workspace/fix-errors/lesson2.js:2:22.** \
**So the error is in the fix-errors folder and in the lesson2.js file.**\
**The first number after the file location is the line there error is located in the file and the second number is the character on that line.**\
&#x20;**So in this case we have an error in the fix-errors folder in the lesson2.js file on line number 2 at character number 2. The character location just means how far over in the line the error is located. The error in this case is names is not a declared variable, or not in the scope of our code when executed, so an error is thrown because the computer cannot find any variable to reference, hence the error type being a reference error.**

## **Error Types**

### **Syntax Error**

**Syntax or format errors are a major problem for most beginning programmers.  A syntax error occurs when the programmer fails to obey one of the grammar rules of the language.  Typically this involves things like using the wrong case, putting punctuation where it’s not supposed to be or failing to put punctuation where it is supposed to be, etc.**

**Common Causes Include:**

* **Javascript:  Unclosed or not matching curly braces, brackets, quotes and parentheses**  \
  **{  },   \[ ], “ ” ,    ( ) all of these need to be a matching pair.**
* **HTML   Unclosed or not matching element tags,   <> \</>** &#x20;
* **A simple typo in the middle of the code.**

### **ReferenceError**

**Reference Errors simply means the program cannot find a variable or access the variable.**&#x20;

**Common Causes Include:**

* **Variable has not been created at that moment when the code is trying to use the variable..**
* **Variable is out of scope**

### **Type Errors**

**Type Errors occur when we try to access properties or methods that do not exist on a data type.** \
**An extremely common type error is “Cannot read property 'length' of undefined”.  This error does not mean length is undefined, but rather that wherever we tried to use the .length property on a variable or data before it was defined.  Undefined does not have any properties so trying to use .length or any property for that matter on undefined throws an error.**

**Common Causes Include**

* **Variable is not defined or does not have a value yet**
* **Trying to access a property or method that does not exist on this data type**

## **Strategies to Fix Errors**

* **If you are having a hard time of locating the exact location you can comment out blocks of your code until it runs. Then slowly uncomment out blocks of code. This is more useful when you have large files with lots of code.**
* **Confirm what your knowledge with console.logs. Never assume your code works or a variable has a value always check the flow of data and variables values with console.logs.**&#x20;

## **Error Prevention**

**We will never be able to fully prevent errors when we code, besides that would make coding too easy.**\
&#x20;**It is very important to work slowly and confirm a function or block of code is working how you expect before moving onto another part of the problem. Simple baby steps is the best way to prevent code errors. For example when assigning a variable a value,  console.log the variable then run your code so we can confirm the following:**

* **That code block, or function is even being executed**
* **The variable is in scope or accessible to that function**
* &#x20;**The variable has the value you expect**

**Breaking problems down into the smallest possible steps. We want to confirm each step mentally using console.log() before moving on to write new code.** \
&#x20;**It is also much easier to fix your errors when you move slowly and double check your work. If you assume your function or block of code works without testing it and then you begin to write new code that uses that function or code,  it can become quite difficult to track down the real errors. We want limit the amount of error possibilities to check and this can be prevented by just double checking your work before moving on.**\
&#x20;  **Lastly write your syntax first handle the matching first before you code logic. For example all  of these have matching pairs. { }, ( ), \[ ] , ‘ ‘ , “ “ . Write the syntax first, then your logic. Make it a habit and you will not have to think about it and you can focus on the actual code problem and not syntax.**
