1

I was hoping for a very concise

video = document.querySelector('#video') ?? throw new Error("Unable to find #video");

But that gives me a "Uncaught SyntaxError: Unexpected token 'throw'"

Is there an easy way to natively do this with the ?? or ?. operators?

Benjamin H
  • 5,164
  • 6
  • 34
  • 42
  • 1
    `throw` isn’t an expression. There’s a [proposal](//github.com/tc39/proposal-throw-expressions) to change that. – Sebastian Simon Oct 13 '22 at 22:37
  • 2
    You can create a reusable helper function, like so: `const throwError = (msg) => { throw new Error(msg); }; const video = document.querySelector('#video') ?? throwError("Unable to find #video");` – Peter B Oct 13 '22 at 22:47

0 Answers0