throw

noun

say it: “THROH” /θroʊ/

A statement that stops the current function on purpose and hands an error to the nearest catch, as in throw new Error("Servings must be more than 0").

What does throw do in JavaScript?

throw — A statement that deliberately raises an exception object.

function reserve(seats) {
    if (seats < 1) {
        throw new Error("Seat count must be positive");
    }
}

In a sentence

  • “If servings is 0, the function will throw an error instead of returning a wrong recipe.”

Level: Journeyman · More JavaScript terms at this level

Updated

In other subjects