DEV Community

artydev
artydev

Posted on

Throw error on missing arguments

In one of his posts Krasimir Tsonev showed a nice tips to "force" passing arguments to a javascript function.

Here is an example :

//https://artydev.github.io/mvu/

const {html, render} = mvu;

function MissinArgException (message) {
  this.message = message;
}

function Error(msg) {
  throw new MissinArgException(msg);
}

function greet (
  user, 
    connected = Error("connected missing")
  ) 
  {
    return html`
      <div>
        ${
          connected 
            && html`<h2>Hello ${user}</h2>` 
            || html`<h2>${user} is not logged</h2>`
         }
      </div>`
}

try {
  render(greet("Marcel", true), document.body)
}
catch (error) {
  alert (error.message)
}
Enter fullscreen mode Exit fullscreen mode

You can try if here ArgMissing

Remove the 'connected' argument and see what happens...

Top comments (0)