JavaScript Program: Catching and handling ReferenceError with the try-catch Block

bookmark

function access_Variable() {
  try {
    console.log(undefinedVariable);
  } catch (error) {
    if (error instanceof ReferenceError) {
      console.log('ReferenceError:', error.message);
    } else {
      console.log('Error:', error.message);
    }
  }
}

// Example:
access_Variable();

 Output:
"ReferenceError:"
"undefinedVariable is not defined"