reading-notes

View on GitHub

Call Stack and Debugging

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. link.

Debugging

link

Running javascript code for the first time could result some errors, and not always we expect the output we want.
for checking your code status in some place to know where the logical error occurs, this is called debugging.

when you run the code, if javascript statement generates an error the interpreter will look for the line of code that generates that error and looks if this line handled the exception, if not and it was inside a function scope, the interpreter will continue looking for the line that called this function and check if it handled the exception or not, if not it will keeping doing the process until the line that handled that exception, or it will generates an error object if it gets to the global scope and the exception did not get handled.
javascript error object has the following:
name: type of execution message: description fileNumber: file name of javascript file lineNumber: line number of the error.
there are 7 types of errors:

Object Description
Error Generic error, all errors based on this object
SyntaxError if the javascript syntax(rules) hasn’t been followed
ReferrenceError trying to use variable from within another scope
TypeError when an unexpected data type that can not be coerced
RangeError number not in acceptable range
URIError encodeURI() and decodeURI() used incorrectly
EvalError eval() function used incorrectly



There are two ways of solving the errors:
Debugging and handling errors.
for debugging method many steps need to follow:

in order of logging message to the console, use the following methods:

each of these methods used for logging to the console with different colors, all beside the console.log(message) method.

for handling exceptions:
if you know that the error might fail, use the try, catch and finally.
each one of them has its own block of code, where the try blocks has the suspicious code within, the catch block will be executed once the code in try block fail, and the finally block will run in both ways, if the code of try block successfully run or not.

function division(){
  var n1 = prompt('Enter a number');
  var n2 = prompt('Enter another number for division');
  var result = 0;
  try{
    result = n1 / n2;
  }catch(e){
    console.log('error message: ' + e.name + e.message);
  }finally{
    return result;
  }
}
console.log(division());