How not to print the line break using the console.log

Asked

Viewed 3,588 times

2

  • I’m developing a program in Javascript where I have to sort an array, only I have a problem at the time of printing that array. I need to print everything in a row, with a space between the elements, and no space at the end, but if I use the console log.() it already breaks the line automatically, how to proceed ?
  • 2

    It seems to me what you want to do is console.log(array.join(" ")) ?

  • What is it ? explains me

  • join creates a string with all elements in it separated by separator you indicate, in case I used the space. All the answers you received have useful information that you should absorb, but it seemed to me that’s what you were trying to do, given the context.

  • Man, thanks is exactly what I wanted.

  • Give it as an Isac response.

  • But this does not answer what you are asking hehe. The question that is asked is another, which is if you give console.log without breaking the line.

  • kkk ok then :/

  • 1

    @Isac But you can post yes if you tweak the answer text. Tell him that the way he asked you to do it, but that by doing it the way you suggested, you should attend to his needs. Sometimes we have to answer not exactly what the author of the question actually asked but rather what solves the problem for him.

  • @Victorocv you tell how to accept?

  • I’ve learned already. v

Show 5 more comments

2 answers

6


It is not possible, not least because this was created to help the debug of your application, so no special formatting is required. This function does not print on screen.

If you still want to do something in this sense the proper way is to create the string all before and issue the print only after everything ready, then you do not put the break in each concatenation that you want. You can even create a sophisticated function that does this.

I’d set a better example if the question had the code that needs this, but it would be something like this:

console.log("Exemplo de texto " + variavel + " continua o texto " + "já isso não faz sentido porque a concatenação pode ser eliminada, mas isso pode ser útil: " + var1.toString() + var2.toString());

In a loop:

var texto = "";
for (var i = 0; i < var.length; i++) texto += var[i] + " ";
console.log(texto);

I put in the Github for future reference.

Solutions that do not use the console.log() will print on screen, so it does not solve your reported problem.

3

The instruction console.log has the purpose of debugging and logging. It does not serve to show the output of your program, it is not for that that she was designed.

To show some text in the output, considering that you are using javascript, there are several ways you can use. For starters, try any of the following:

1.

var saida = ...;
document.write(saida);

2.

var saida = ...;
alert(saida);

3.

var saida = ...;
document.getElementById("algum-elemento-no-html").innerHTML = saida;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.