Javascript for loop console print in one line

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);

Build a string then log it after the loop.

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

No problem, just concatenate them together to one line:

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

or better,

console.log(Array.apply(null, {length: 10}).map(function(el, index){
   return index;
}).join(' '));

Keep going and learn the things! Good luck!


In Node.js you can also use the command:

process.stdout.write()

This will allow you to avoid adding filler variables to your scope and just print every item from the for loop.