ScanSkill

throw

The throw statement throws a user-defined exception.

Syntax

throw expression;

The throw statement throws an error from the point it is invoked at.

Example

const number = "this is not a number";

if(isNaN(number)) {
  throw "Please only enter a number";
}
// Prints: Please only enter a number

The throw statement halts the whole program, if not used with the try and catch statement.