PASSWORD RESET

Your destination for complete Tech news

how to print in console without a trailing newline?

6.41K 0
< 1 min read

To print text to the console without a trailing newline in Node.js, you can use the process.stdout.write() function instead of the console.log() function.

The process.stdout.write() function writes a string to the standard output stream (stdout) without adding a newline at the end. This allows you to print multiple strings on the same line in the console.

Here’s an example of how to use the process.stdout.write() function to print to the console without a trailing newline:

process.stdout.write('This is the first string');
process.stdout.write('This is the second string');

In this example, both strings will be printed on the same line in the console.

You can also use the console.log() function with a template string and the %s placeholder to achieve the same effect:

console.log('%s %s', 'This is the first string', 'This is the second string');

In this example, both strings will be printed on the same line in the console.

Note: To add a newline after printing a string, you can use the process.stdout.write() function with the \n escape sequence, or you can use the console.log() function as usual.

process.stdout.write('This is the first string\n');
console.log('This is the second string');

In this example, the first string will be printed on one line, followed by a newline, and the second string will be printed on the next line.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.