Difference between console.log and process.stdout.write in NodeJS

Node.jsServer Side ProgrammingProgramming

Both the methods – console.log and process.stdout.write have a basic definition to write or print the statements on the console. But, there is a slight difference in the way they execute these tasks. Internally, console.log implements process.stdout.write which itself is a buffer stream that will be used to directly print statements on your console.

process.stdout.write
console.log
It continuously prints information as retrieved from the stream without adding any new line.
It first prints the information being retrieved and then adds a new line. Then it will go to retrieve the second set of statements to print.
The process.stdout.write method takes only string as paramter. Other datatypes passed will result in a type error.
It can take any type of input parameter.
We will get a weird characted if we don't put the breakline at the end.
We donot need any break line here as the text is already formatted, also the character disappears itself.
It is used for printing patterns, since it does not adds a new line.
It is used when we require a new line after printing a statement.
It cannot be used to print multiple strings at a time.
For example, process.stdout.write("Hello", "World"); will throw a type error as it is not a string anymore.
We can write multiple strings using this method. For example, console.log("Hello", "World"); will print Hello World in console.
This method cannot associate two string types. For example,
process.stdout.write("Hello %s", "All"); will throw a type error/
This method can be used for making associations.
For example, console.log("Hello %s", "All"); will print Hello All on the console.

process.stdout.write

Example 1

<script>
   // For process.stdout
   process.stdout.write("Hello");
   process.stdout.write("World");
   process.stdout.write("!!!");
</script>

Output

HelloWorld!!!
console.log

Example 2

<script>
   // For process.stdout
   console.log("Hello");
   console("World");
   console.log("!!!");
</script>

Output

Hello
World
!!!
raja
Published on 27-Apr-2021 12:55:12
Advertisements