Native Assertions

T.J. Crowder tj.crowder at
Mon Jan 14 11:37:40 UTC 2019

On Sun, Jan 13, 2019 at 6:49 PM Cyril Auburtin
<cyril.auburtin at> wrote:
> There's `console.assert`

The problem with that and other userland solutions is that for something
like this:

assert(a !== b, `a (${a}) !== b (${b})`);

you have only three choices for production builds:

1. Have the asserts active in production (throwing assertion errors).

2. Have the asserts do the relevant check and process the template literal,
but not raise an error when it fails. (So `a !== b` still gets executed,
but no error is raised if it's false.)

3. Pre-process your scripts to remove the assertions

Whereas with a language-level assert, in production that entire
pseudo-function-call would be parsed but then completely ignored, including
the `a !== b` and evaluating the template. And ideally, with a
language-level feature, even when assertions are enabled, the template
would only be evaluated if the assertion failed, as though you'd written:

if (a !== b) {
    throw new AssertionError(`a (${a}) !== b (${b})`);

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list