JavaScript equivalent of PHP’s die

Viewed 157904

Is there something like "die" in JavaScript? I've tried with "break", but doesn't work :)

14 Answers

you can try with :

return;   

that work in case of stop process.

There is no function exit equivalent to php die() in JS, if you are not using any function then you can simply use return;

return;

Global die() function for development purposes:

var die = function(msg) {
    throw new Error(msg);
}

Use die():

die('Error message here');

This should kind of work like die();

function die(msg = ''){
    if(msg){
        document.getElementsByTagName('html')[0].innerHTML = msg;
    }else{
        document.open();
        document.write(msg);
        document.close();
    }
    throw msg;
}
Related