Node.js 中 process.stdout.write 和 console.log 的区别
NodeJS 中的process.stdout.write和console.log都具有在控制台上显示消息的基本功能。基本上console.log实现process.stdout.write而process.stdout.write是一个缓冲区/流,将直接在我们的控制台中输出。
Node.js 中 process.stdout.write 和 console.log 的区别是:
Sl no. | process.std.out | console.log |
1 | It continuously prints the information as the data being retrieved and doesn’t add a new line. | It prints the information that was obtained at the point of retrieval and adds a new line. |
2 | Using process.stdout for a variable that shows an object. | Using console.log for a variable shows a lot of unreadable characters. |
3 | It only takes strings as arguments. Any other data type passed as a parameter will throw a TypeError. | It takes any JavaScript data type. |
4 | If we don’t put the break line at the end we will get a weird character after our string. | We don’t need the break line here because it was already formatted and also that weird character did disappear |
5 | It can be useful for printing patterns as it does not add a new line. | It is used when we want our result to be printed in a new line. |
6 | We can not write more than one string. For example: | We can write more than one string. For example: |
7 | We can not make associations. For example: | We can make associations. For example: |
示例:下面的示例是展示process.stdout.write的使用
Javascript
Javascript
输出:
HelloWorld!!!
示例:下面的示例是展示如何使用console.log
Javascript
输出:
Hello
World
!!!