Javascript for loop console print in one line

前端 未结 6 1962
时光取名叫无心
时光取名叫无心 2020-12-29 14:02

I\'m trying to get the output from my for loop to print in a single line in the console.

for(var i = 1; i < 11; i += 1) {
    console.log(i);
}

6条回答
  •  灰色年华
    2020-12-29 14:37

    There can be an alternative way to print counters in single row, console.log() put trailing newline without specifying and we cannot omit that.

    let str = '',i=1;
    while(i<=10){
        str += i+'';
        i += 1;
    }
    
    console.log(str);

提交回复
热议问题