throw in javascript allows to create custom or user-defined error message, while using try catch to deal with runtime errors. Javascript provides error message in more technical terms, to make error message more understandable and friendly to user, use throw statement.

throw, error-message can be of any type, string, character, number, decimal, object or boolean.

Syntax

throw expression/error-message

Example

<script>
try {
  let a = 120;

  if(a > 20)
   {
      throw "value is greater";
   }
   else{
       // your logic
    }

} catch (er) {
   document.write(er.message);
}

In above example, if value of a is greater than 20, it throws error message.