Back to Freecodecamp

Learn to Solve Problems and Understand Errors Lesson F

curriculum/challenges/english/blocks/top-learn-to-solve-problems-and-understand-errors/66581a7db1eb2281159492ff.md

latest1.7 KB
Original Source

--description--

Another important part of an error is the stack trace. This helps you understand when the error was thrown in your application, and what functions were called that led up to the error. So, for example, if you have the following code:

javascript
const a = 5;
const b = 10;

function add() {
  return c;
}

function print() {
  add();
}

print();

Our function print() should call on add(), which returns a variable named c, which currently has not been declared. The corresponding error is as follows:

The stack trace tells us that:

  1. c is not defined in scope of add(), which is declared on line 5.
  2. add() was called by print(), which was declared on line 9.
  3. print() itself was called on line 12.

Thus the stack trace lets you trace the evolution of an error back to its origin, which here is the declaration of add().

--questions--

--text--

How does the stack trace help in debugging the error in the provided JavaScript code?

--answers--

The stack trace shows that the function add() correctly returns the value of c.


The stack trace indicates which line in the code needs to be edited to correct a syntax error.


The stack trace provides a detailed path of function calls leading to the error, helping identify where c is incorrectly referenced.


The stack trace only indicates that print() was called, but does not provide details about the error in add().

--video-solution--

3