DEV Community

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

Jan Küster 🔥 on February 18, 2022

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 ...
Collapse
 
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 {
  e()
} 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.

Collapse
 
jankapunkt profile image
Jan Küster 🔥

Without depending on the input but more lines

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