DEV Community

Jan Küster
Jan Küster

Posted on

JavaScript one-liner puzzle: throw an error without using throw

Write a function that throws an error. Contstraints:

  • do not use throw
  • one line only
  • simply returning an Error is not sufficient (since it's not thrown)
  • function declaration must not throw the error, only execution should throw
  • Syntax error is not allowed!
  • max. ovarall characters for the code: 15


  • the function is not something you should ever use in your code
  • think of fundamentals and what you learned about them
  • it's marked #beginners, because it's way easier than you might think
  • you can make pretty weird things with JavaScript

Top comments (2)

thormeier profile image
Pascal Thormeier

Uuh, love me some code golf!

Shortest implementation of any error I could think of would be something like this:

let e=_=>_._
Enter fullscreen mode Exit fullscreen mode

12 characters.

And then using it like so:

try {
} catch (err) {
  console.warn('error thrown', err)
Enter fullscreen mode Exit fullscreen mode

Throws Cannot read properties of undefined (reading '_'), though there's no error if the function receives an object as argument with _ defined.

jankapunkt profile image
Jan Küster

Without depending on the input but more lines

const f=()=>f=0
Enter fullscreen mode Exit fullscreen mode