Home > Net >  New line character does not work with other parameters in javascript
New line character does not work with other parameters in javascript

Time:08-17

I just basically started learning JS and stuck on this problem, thank you for your help.

I created an array which I refer to in console.log and everythink worked, until I wanted to add a string with new line.

Here's my code:

var array = [[1,2,3,4,5],[7,8,9,0]];
console.log(array[0], array.length, "hello \n world");

After I turn on the program, google console shows something like this:

(5) [1, 2, 3, 4, 5] 2 'hello \n world'

And the " 'hello \n world' " is red if it matters.

I also tried with " " instead of ",": but also didn't work, can anyone help? Why the "world" isn't in the second line?

CodePudding user response:

var array = [[1,2,3,4,5],[7,8,9,0]];
console.log("\(" array[0].length "\)"   JSON.stringify(array[0]), array.length, "hello world");

It's working as expected, Please comment if you need something else in output.

output

CodePudding user response:

Console log is meant as a debugger, not really for formatting/formatted text. I guess its approach is to try to show you what is being logged, not to render it in any way.

Out of curiosity why would you want to have a line break in your logged data?

enter image description here

CodePudding user response:

Try providing with formatting string and then values

v1 = "foo";
v2 = "boo";
console.log("%s\n%s", v1, v2);

  • Related