Search code examples
javascriptnode.jsejs

EJS alternative to PHP die()


I just want to know if there is a way to replicate this PHP code in EJS view:

<?php
  // ...
  if ($something) {
    echo 'something is true';
    die;
  }
  // Now I can continue with my code without being in "else" clause.

I feel like there is really simple solution to this but I just can't seem to find it.

Thanks in advance.


Solution

  • Not really sure if this is a good idea, but the closest you can come to the desired syntax in JavaScript is using a labelled block:

    myLabel: if(true) {
      console.log('before break');
      break myLabel;
      console.log('after break');
    }

    Running the example, you can see that "before break" logs, but "after break" does not.

    Please note that I do not condone this pattern, and that the break will not affect any code outside the labelled block.