CH-SO
CH-SO

Reputation: 209

Javascript for loop console print in one line

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

Right now it's

1
2
3
4
5
6
7
8
9
10

How can I get the output all in one line (like this 1 2 3 4 5 6 7 8 9 10)?

Upvotes: 20

Views: 128139

Answers (11)

Mukadas Maltiti
Mukadas Maltiti

Reputation: 83

A non-conventional method; add the elements into an array and use the spread operator to spread your values in a single console log

const arr = []
for (let i = 1; i <= 5; i++){
    arr.push(i)
}
console.log(...arr)

Upvotes: 0

mustaquenadim
mustaquenadim

Reputation: 81

Alternatively, you may also try this like

Program

let s = '';
for(let i = 1; i < 11; ++i) {
  s += `${i} `;
}
console.log(s);

Output

1 2 3 4 5 6 7 8 9 10 

Upvotes: 0

Nilin
Nilin

Reputation: 1

Just change console.log() into process.stdout.write().
nb : if you are using Nodejs

Upvotes: 0

Ayush Rudani
Ayush Rudani

Reputation: 11

We can use the process.stdout.write() method to print to the console without trailing newline. It only takes strings as arguments, but in this case i + " " is a string so it works:

for (var i = 1; i < 11; i += 1) {
  process.stdout.write(i + " ");
}

Note: It will work only with Node.js.

Upvotes: 0

Marco Paulo
Marco Paulo

Reputation: 29

Alternatively, to print in a single row you can use repeat in Javascript-

for(let j = 0; j < 5; j++){
    console.log('*'.repeat(j))    
}

Upvotes: 0

Manonmani R
Manonmani R

Reputation: 11

let n=0;

for ( i = 1; i <= 10; i++)
    {
      n += i + “ “;
      console.log(n);
}

Upvotes: 1

user11073489
user11073489

Reputation: 157

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.

Upvotes: 11

Yiin
Yiin

Reputation: 659

// 1 to n
const n = 10;

// create new array with numbers 0 to n
// remove skip first element (0) using splice
// join all the numbers (separated by space)
const stringOfNumbers = [...Array(n+1).keys()].splice(1).join(' ');

// output the result
console.log(stringOfNumbers);

Upvotes: 1

jasmeetsohal
jasmeetsohal

Reputation: 141

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

Upvotes: 1

Artemis
Artemis

Reputation: 4851

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!

Upvotes: 5

Dave
Dave

Reputation: 10924

Build a string then log it after the loop.

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

Upvotes: 37

Related Questions