3

I am looking for a way to print out a given variable f.ex. the i of a for-loop for each iteration, without it resulting in the entire console being filled with new lines of new values. Is there a console. method for just printing one line, and updating that as we go?

I realise that you could do this by implementing a text-field in your program which you change the with each iteration, but if there is a way of doing this in the console it would be a bit easier (and perhaps quicker? although I am really not sure about that). Thanks in advance.

If there is still confusion about what im asking, what i want is my console to print out: "i = " i once, and then update the i in that one line, instead of:

i=1
i=2
i=3
1=4
.
.
.

which gets really messy as you go. For the exact example of the i in a for loop, you could get this value from just console.log()'ing the same thing for each iteration, and a number will pop up beside it (in firefox anyway), but i would like to be able to do this with some more useful information.

Jorgen
  • 93
  • 1
  • 8
  • 2
    In a word, no... – Heretic Monkey Jun 09 '19 at 01:32
  • 2
    If you feel the console is being spammed why not just use `console.clear();` It would be one of many good reasons for this function existing... – NewToJS Jun 09 '19 at 01:39
  • There's no way to do it that I'm aware of. But if you want to count and have a clean method try `console.count("label:" + i );` – EasyBB Jun 09 '19 at 01:39
  • 1
    Possible duplicate of [Chrome JavaScript developer console: Is it possible to call console.log() without a newline?](https://stackoverflow.com/questions/9627646/chrome-javascript-developer-console-is-it-possible-to-call-console-log-withou) – niry Jun 09 '19 at 01:53
  • Is logging every `i` of all loop iteration a best way to achieve your goal? For example, if it's possible I would prefer something like (inside loop) `console.log(condition? i : undefined)` (after loop) `console.log(`iteration count: ${i}`)` – ik1ne Jun 09 '19 at 01:56

2 Answers2

1

Option 1: use console.groupCollapsed() and console.groupEnd():

    console.groupCollapsed();
    for (let i = 0; i < 100; i+= 1) { console.log(`i = ${i}`) }
    console.groupEnd();

Option 2: set the values in an array or a string and log the var when the iterations finish:

    let valuesToLog = [];
    for (let i = 0; i < 100; i+= 1) { valuesToLog.push(`i = ${i}`) }
    // see it as an array
    console.log(valuesToLog);
    // see it as a string, with each value separated by ', '
    console.log(valuesToLog.join(', '));
Nico Diz
  • 1,484
  • 1
  • 7
  • 20
-1

how about JQuery console.clear()?

$(function(){
    loop_counter();
});

function loop_counter() {
    for (let i = 1; i <= 100; i++){
        console.clear();
        console.log ("i=", i)
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Marco Sanchez
  • 788
  • 7
  • 22
  • 4
    Why include `jQuery` for such a simple task? Also the OP hasn't tagged `jQuery` – NewToJS Jun 09 '19 at 01:39
  • Problem with this it will look like a garbled mess and use more resources then needed. Clear object is a good option but not in a for loop. – EasyBB Jun 09 '19 at 01:41