user8157430
user8157430

Reputation:

javascript use console.log without moving to new line

I am new to js and hope this is not too trivial, but I am unable to find any help on the net. I wish to output to console.log and prevent moving to a new line, so the next time the output will be appended to the same line. ie,

"use strict";
for (let i = 0; i<=9;i++){
     console.log(i);    // here i would like to freeze the output so the result is                     0123456789 on one line, rather than those digits in a column.
}

I have seen fixes involving assigning the outputs to a string and printing in 1 hit, but that seems incredibly crude. Even in Fortran 4 as I recall in the '70s, you could prevent moving to a new line before printing again, so I think I am missing something fundamental. Also I cannot find any general help on formatting numerical output in javascript. Can someone point me in the right direction? Thanks

Upvotes: 3

Views: 4651

Answers (2)

Rion Williams
Rion Williams

Reputation: 76547

Unfortunately, the console.log() method will only write out a string to a single line and doesn't support the appending behavior you are looking for.

As you detailed in your original post, you could accomplish writing the final result out through the use of a variable (i.e. displaying the final concatenated string), but not continually appending to the same line within the console itself as the loop is being iterated over.

Alternative Grouping Option

The concept of grouping entries is supported, which is obviously very different than your original ask, but it may be worth considering as mentioned in the documentation for console.group() and might look something like this:

var rollingConcatenation = '';
console.group("Looping Group Example");
for (let i = 0; i<=9;i++){
     rollingConcatenation += i;
     console.log(rollingConcatenation);    
}
console.groupEnd();

This can give your console the following appearance, which can help with readability (depending on your use cases):

in

Do It Yourself Implementation

Another option might be to store your current console value within a variable and at clear it and rewrite the updated values out. Depending on your very specific use cases, you could achieve the behavior you are looking for using something like this crude implementation:

// Define a custom console
var customConsole = {
  // Store a reference to your backing value
  tempValue: '',
  // Always write out the most recent value
  log: function(msg) {
    this.tempValue += msg;
    console.clear();
    console.log(this.tempValue);
  },
  // A clear method to clear the backing console
  clear: function() {
    this.tempValue = '';
    console.clear();
  }
}

for (var i = 0; i < 10; i++) {
  // Use your custom console instead of the normal one
  customConsole.log(i);
}

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Take a new variable outside the loop and then prepare that string inside the loop and then you can console.log() outside the loop.

var str = '';
for (let i = 0; i <= 9; i++) {
  str += i;
}
console.log(str);

Upvotes: -2

Related Questions