Notes for Codefellows Code 401 301 201 and 102
resources The Call Stack defined on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors
Understanding the JavaScript Call Stack https://codeburst.io/javascript-error-messages-debugging-d23f84f0ae7c
JavaScript error messages https://www.freecodecamp.org/news/understanding-the-javascript-call-stack-861e41ae61d4/
Additional Resources JavaScript errors reference on MDN https://developer.mozilla.org/en-US/docs/Glossary/Call_stack
A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.
When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function. Any functions that are called by that function are added to the call stack further up, and run where their calls are reached. When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing. If the stack takes up more space than it had assigned to it, it results in a “stack overflow” error. Summary:
we start with an empty Call Stack. Whenever we invoke a function, it is automatically added to the Call Stack. Once the function has executed all of its code, it is automatically removed from the Call Stack. Ultimately, the Stack is empty again.
Types of error messages:
occurs when we have something that cannot be parsed in terms of syntax
Range errors
Type errors occurs when the types (number, string and so on we are trying to use or access are incompatible, like accessing a property in an undefined type of variable.
To debug JS code, the easiest and maybe the most common way its to simply console.log() the variables we want to check or, by using chrome developer tools.
Using Node.js with Visual Studio Code we can press the debug tab and add a configuration similar to this:
“version”: “0.2.0”, “configurations”:[ { “type”: “node”, “request”: “launch”, “name”: “Launch Program”, “program”: “${workspaceRoot}/index.js” } ] // debugger by pressing F5 Another way to find errors in code, using call stack in the console, which is the path that program has taken to reach the point were we set a breaking point or were we have an error.
For call stack it’s better to navigate when you have names to your functions
Handling errors Good practice to handle errors is try to catch the errors so we can fallback to a default state of our application in case of an error.
(function testing(){ function add(a, b) { try { var result = a + b return result.split(‘’) } catch (error) { console.error(‘add went wrong ->’, error) return [] // default value } } var stringResult = add(“1”, “2”) var numberResult = add(1, 2) })() Tools to avoid runtime errors:
2 find the source of an error it helps to know how scripts are processed.
There is one global execution context; plus each function creates a new execution context. they respond to variable scope;
global context - global variable scope
function context - function level scope
The JavaScript interpreter processes one line of code at a time. When a statement needs data from another function, it stacks (or piles) the new function on top of the current task.
Each time a script enters a new execution context, there are two phases:
In the interpreter each execution context has its own variables object. It holds the variables, functions and parameters available within it. Each execution context can also acess its parents variables object.
functions are linked to the object they were defined within. So for each execution context, the scope is the current execution contexts variables object plus the variables object for each parent execution context. Imagine each function is a nesting doll. The chilkdren can ask their parents for information in their variables. But the parents cannot get variables from their children. Each child will get the same answer from the same parent.
If a JavaScript statement generates an error, then it throws an exception . At that point, the interpreter stops and looks for exception-handling code.
Error objects can help you find where your mistakes are and browsers have tools to help you read them.
There are seven types of built-in error objects in JavaScript.
SyntaxError: Unexpect ed EOF
)SyntaxErr or : Expected token ')'
)ReferenceError: Can't find variable: height
)ReferenceError: Can't find variable: randomFunction
)TypeError: 'undefined' is not a function (evaluating 'Document.write('Oops! ')')
)TypeError: 'undefined' is not a function (evaluating 'document.Write('Oops!')')
)TypeError: 'undefined ' is not a function (evaluating 'box.getArea()')
)TypeError: 'null' is not an object (evaluating 'el .innerHTML = 'Mango'')
)RangeError: Array size is not a small enough positive integer
)RangeError: toFixed() argument must be between 0 and 20
)? & # : ;
First, you should try to narrow down the area where the problem seems to be. In a long script, this is especially important. Look at the error message, it tells you:
Once you think that you might know the rough area in which your problem is located, you can then try to find the actual line of code that is causing the error.