How to use process.exit for Node.JS applications In Javascript
Example: There will be no other output, in this case since we exited the script. This is applicable to all JavaScript applications in the NodeJS environment. There are numerous other methods such as process.kill(process.pid) and process.abort() in NodeJS but process.exit() suffices for most of the cases, if not all cases.
javascript
function doSomeThing() { let i = 10; console.log( 'Terminating ' ); process.exit(0); let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log( 'This section will not be executed' ); console.log(arr.filter( (elem, index) => { return elem > 2 })); } doSomeThing(); |
Output:
Terminating:
How to terminate a script in JavaScript ?
To terminate a script in JavaScript, we have different approaches:
Table of Content
- Using return
- Terminating a part of the Script
- Throwing an error on purpose
- Using process.exit for Node.JS applications
- Using clearInterval() method
Contact Us