How to Create Line Breaks in Console.Log() in Node.Js

How to create line breaks in console.log() in Node.js

I have no idea why this works in Node.js, but the following seems to do the trick:

console.log('', a, '\n', b, '\n', c)

Compliments of theBlueFish.

LineBreak in console object

The answer is no: when you print an object to the console log, strings will be written as javascript objects (similar but not identical to what you'd get if you explicitly converted them into JSON, like console.log(JSON.stringify(object))).

If you want for some reason to print your strings with line breaks, you'd have to implement the object-to-string conversion yourself; perhaps with something like this:

function customString(object) {
let string = '{\n';
Object.keys(object).forEach(key => {
string += ' "' + key + '": "' + object[key] + '"\n';
});
string += '}';
return string;
}

console.log(customString({ value: "Foo\nBar" }));

(It sounds like you have an idea in mind of exactly how you want this output to look, so adjust the function above until it works as expected.)

Node.js: printing to console without a trailing newline?

You can use process.stdout.write():

process.stdout.write("hello: ");

See the docs for details.

Is there anyway to console.log without a newline?

You could spread the array. Then all values are taken as parameters.

let array = [1, 2, 3, 4, 5];
console.log(...array);

When tracing out variables in the console, How to create a new line?

In ES6/ES2015 you can use string literal syntax called template literals. Template strings use backtick character instead of single quote ' or double quote marks ". They also preserve new line and tab

const roleName = 'test1';const role_ID = 'test2';const modal_ID = 'test3';const related = 'test4';        console.log(`  roleName = ${roleName}  role_ID = ${role_ID}  modal_ID = ${modal_ID}  related = ${related}`);

NodeJS simple horizontal line on console.log

The console does not support rendering HTML elements.

That does not prevent you from making a custom line however!

const lineBreak = '----------------------'
console.log(lineBreak)

Of course, customize the linebreak however you'd like:

______ //Underscores!
----- //Hyphens!
====== //Equals!

For grouping related data, refer to the docs here: console reference

Example:

function name(obj) {
console.group('name');
console.log('first: ', obj.first);
console.log('middle: ', obj.middle);
console.log('last: ', obj.last);
console.groupEnd();
}

name({"first":"Wile","middle":"E","last":"Coyote"});

Will output grouped data to the console, visually giving it a line break & arrow to collapse the group. I think this would work well for your use case.

My string breaks into multiple line in my console.log but does not in my html

I just figured this out by editing the CSS to:

white-space: pre-wrap

this allows \n to be picked up by the html.

So I changed my code to:

data = ["name 1: Bob", "name 2: Steve"]
var dataLined = data.toString().replace(',','\n')
console.log(dataLined)

document.getElementByID('Content').append("Data\n" + dataLined)

Thanks to those that are maybe in progress of helping!

How do I create a line break in a JavaScript string to feed to NodeJS to write to a text file?

Use

var os = require('os');
var jsonText = '{' + os.EOL + '\t"text": "' + origText + '"' + os.EOL + '}';


Related Topics



Leave a reply



Submit