Chrome JavaScript developer console: Is it possible to call console.log() without a newline?

前端 未结 12 851
生来不讨喜
生来不讨喜 2020-11-29 05:07

I\'d like to use console.log() to log messages without appending a new line after each call to console.log(). Is this possible?

相关标签:
12条回答
  • 2020-11-29 05:18

    The short answer is no.

    But

    If your use-case involves attempting to log perpetually changing data while avoiding console-bloat, then one way to achieve this (in certain browsers) would be to use console.clear() before each output.

    function writeSingleLine (msg) {
    
      console.clear();
      console.log(msg);
    
    }
    
    writeSingleLine('this');
    setTimeout( function () { writeSingleLine('is'); }, 1000);
    setTimeout( function () { writeSingleLine('a'); }, 2000);
    setTimeout( function () { writeSingleLine('hack'); }, 3000);

    Note that this would probably break any other logging functionality that was taking place within your application.

    Disclaimer: I would class this as a hack.

    0 讨论(0)
  • 2020-11-29 05:24

    If your only purpose to stop printing on many lines, One way is to group the values if you don't want them to fill your complete console

    P.S.:- See you browser console for output

    let arr = new Array(10).fill(0)
    
    
    console.groupCollapsed('index')
    
    arr.forEach((val,index) => {
      console.log(index)
    })
    
    console.groupEnd()

    console.group

    console.groupCollapsed

    0 讨论(0)
  • 2020-11-29 05:25

    You can use a spread operator to display output in the single line. The new feature of javascript ES6. see below example

       for(let i = 1; i<=10; i++){
            let arrData = [];
            for(let j = 1; j<= 10; j++){
                arrData.push(j+"X"+i+"="+(j*i));
            }
            console.log(...arrData);
        }
    

    That will print 1 to 10 table in single line.

    0 讨论(0)
  • 2020-11-29 05:27

    if you want for example console log array elements without a newline you can do like this

    const arr = [1,2,3,4,5];
    
    Array.prototype.log = (sep='') => {
        let res = '';
        for(let j=0; j<this.lengthl j++){
            res += this[j];
            res += sep;
        }
        console.log(res);
    }
    
    // console loging
    
    arr.log(sep=' '); // result is: 1 2 3 4 5 
    
    0 讨论(0)
  • 2020-11-29 05:29

    Something about @shennan idea:

    function init(poolSize) {
          var pool = [];
          console._log = console.log;
          console.log = function log() {
            pool.push(arguments);
            while (pool.length > poolSize) pool.shift();
        
            draw();
          }
          console.toLast = function toLast() {
            while (pool.length > poolSize) pool.shift();
            var last = pool.pop() || [];
            for (var a = 0; a < arguments.length; a++) {
                last[last.length++] = arguments[a];
            }
            pool.push(last);
        
            draw();
          }
          function draw() {
            console.clear();
            for(var i = 0; i < pool.length; i++)
              console._log.apply(console, pool[i]);
          }
        }
        
        function restore() {
          console.log = console._log;
          delete console._log;
          delete console.toLast;
        }
        
        init(3);
        console.log(1);
        console.log(2);
        console.log(3);
        console.log(4);    // 1 will disappeared here
        console.toLast(5); // 5 will go to row with 4
        restore();

    0 讨论(0)
  • 2020-11-29 05:31

    In NodeJS you can use process.stdout.write and you can add '\n' if you want.

    console.log(msg) is equivalent to process.stdout.write(msg + '\n').

    0 讨论(0)
提交回复
热议问题