concept exit code in category node

appears as: exit codes, exit code, n exit code
Node.js in Action, Second Edition

This is an excerpt from Manning's book Node.js in Action, Second Edition.

Imagine you want to log a message only when JSON is successfully parsed. To do this, you need new operators. The && and || operators act similarly in a shell to the way they do in JavaScript when used on numbers. Using && executes the next command if the previous exit code is zero, and || executes the next command if the exit code is a nonzero number.

11.5.2. Working with errors and exit codes

Right now the program doesn’t output anything. But if you give it incorrect data, how do you know that it was able to complete successfully, even if you don’t know the expected output of an executable? The answer is the exit code. You can see the exit code of the last command you ran, but note that the echo and node commands are treated as a single command unit because of piping.

On Windows, you can inspect the exit code with the following:

echo %errorlevel%

On UNIX, you can see the exit code by using this command:

echo $?

If a command is successful, it has an exit code of 0 (zero). So if you feed incorrect JSON to the script, it should exit with a nonzero value: