How to append new information and rethrowing errors in nested functions in JavaScript?

We can append new information to errors by creating a new error object with the original error as its prototype and adding additional properties. This allows us to maintain the original error message while also providing additional context.

JavaScript Functions

In JavaScript, a function is a block of code that is executed when it is invoked. Functions are declared with the function keyword. Functions can take arguments. The arguments are the values that are passed to the function when it is invoked.

Functions can return values. The value that is returned by the function is the value that is assigned to the function invocation expression. It can be invoked with the function name followed by parentheses. The parentheses can optionally contain the values for the arguments.


  • When appending new information to a nested function in JavaScript, the approach is to first check if the function exists, and if it does, to append the new information to the existing function.

  • If the function does not exist, then the approach is to create the function and then append the new information.

  • When rethrowing errors in nested functions, the approach is to first check if the error is recoverable, and if it is, to try to recover from the error.

  • If the error is not recoverable, then the approach is to throw the error up to the next level.


Adding new information −

function foo() {
   // Statements
   console.log('called inside foo');
}; = function() {
   // Statements
   console.log('called inside bar of foo');


  • To append new information, simply add the new information after the existing information.

  • In this example, a new function called “bar” is added to the “foo” function.

  • To run this example, simply call the “foo” function with the new “bar” function attached.


Updated on: 16-Feb-2023


Kickstart Your Career

Get certified by completing the course

Get Started