How to not print the line break using console.log

1
  • I'm developing a program in Javascript where I have to sort an array, but I'm having a problem printing this 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?
asked by anonymous 03.08.2018 / 00:38

2 answers

5

It's not possible, not least because this was designed to help with your application's debug , so it does not need special formatting. This function does not print on the screen.

If you still want to do something along those lines, the proper way is to create the string all before and issue the impression only after everything is ready, so you do not put the break in every concatenation < strong> you want. You can even create a sophisticated function that does this.

I would put a better example if the question had the code that needs it, but it would look 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);

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

    
03.08.2018 / 00:43
2

The console.log statement is for debugging and logging purposes. It does not serve to show the output of your program, that's not what it was designed for.

To show some text in the output, considering that you are using javascript, there are several ways you can use it. To get started, 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;
    
03.08.2018 / 00:44