Proposal: throw null operator

# IdkGoodName Vilius (6 months ago)

I am proposing a syntatic sugar, which would check if throw value is not null, or not undefined. It would make less typing and no if, for throw statements. Syntax is simple. Here's an example of it:

throw? e

This is basically equivalent to:

if(e !== null && e !== undefined) throw e

What's the purpose of that? Well, in some node js modules, error as null, or undefined is sometimes passed. Example of that is:

const {exec} = require('child_process')
exec('...', {cwd: '...'}, (err, stdout, stderr) => {
    if(err !== null) throw err
})
# Isiah Meadows (6 months ago)

Not sure what the benefit is over just if (value != null) throw value, especially for this niche of a use case.

# Ranando King (6 months ago)

Wasn't there a proposal that tend to allow for optional access of undefined fields?

let a = { foo:  "bar" };
let b = a.bar?.alpha; // b == undefined

The point, I believe, was to eliminate the field testing sometimes required to find a nested sub-element, and simply return undefined if any branch property wasn't an object.

To me, the OP here is asking for a very similar operation, causing the "throw" action to be abandoned if the parameter doesn't exist, just as in the above example, the [[GET]] operation would be abandoned and return undefined.