CodeBlanks Logo Icon CodeBlanks

process.stdin/stdout

Learn streams with process.stdin/stdout.

process.stdin and stdout

process.stdin and process.stdout are the standard input and output streams. stdin reads data from the keyboard or a pipe; stdout writes output to the screen. console.log is actually a wrapper around stdout.

process.stdout.write("hello") prints the output but, unlike console.log, it does not add a newline at the end. This is useful when you want to write several pieces on the same line. stdin is a Readable stream; you can read incoming data with process.stdin.on("data", cb). This lets you build interactive CLI tools.

write versus console.log

The main difference between process.stdout.write and console.log is the newline. console.log automatically adds \n after each call; write does not.

For example, process.stdout.write("A"); process.stdout.write("B"); gives the output AB, side by side on one line. Doing the same with console.log would show A and B on separate lines. For progress bars, percentage indicators or updating the same line, write is preferred. Because it works as a stream, write is also more efficient for large outputs.

Practice Write two pieces to stdout

Write two pieces side by side with process.stdout.write("Node") and process.stdout.write("JS"). Do not add a newline.

Starter code

index.js
// Write "Node" to stdout
// Write "JS" to stdout
// write here

Sample solution

index.js
process.stdout.write("Node");
process.stdout.write("JS");

Expected output

NodeJS

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What kind of object is process.stdin?

index.js
process.stdin
  • A string
  • Readable stream Correct answer
  • A number
  • An array

Explanation: stdin is a Readable stream; you read data in chunks.

2 Write to stdout

Question
process.____.write("Node");
Answer
process.stdout.write("Node");

Explanation: stdout.write prints without a newline.

3 Match each piece to its role

Term Meaning
stdin Input stream
stdout Output stream
write Writes without newline
console.log Writes with newline

All lessons in Intermediate