4

I want to print a list (var lst=[1,2,3,4,5]) using a loop on the same line. Can I do that in JavaScript ?

7
  • 1
    console.log.apply(null, lst);. Commented Apr 29, 2017 at 18:58
  • 2
    @ANS: no, that will print each item of the list on a separate line/log entry. Commented Apr 29, 2017 at 18:59
  • 1
    What do you mean by "print" and "on the same line"? What is expected result? Commented Apr 29, 2017 at 18:59
  • console.log(JSON.stringify(lst))? Commented Apr 29, 2017 at 19:00
  • 1
    Construct your message as a string before your log it. Simple as that. Commented Apr 29, 2017 at 19:02

6 Answers 6

4

Well you can achieve this by doing this. Make an empty string variable and then concatenate each array value to it, then print it out. This will work

var lst = [1, 2, 3, 4, 5];

var output = "";
for (var i = 0; i <= lst.length; i++){
   output += lst[i] + " ";
}

console.log(output);
Sign up to request clarification or add additional context in comments.

Comments

3

In NodeJS and as long as you didn't change your standard output you can use process.stdout.write("Something here") as the standard output of your application is terminal (console).

process.stdout.write("Hello ")
process.stdout.write("World")
process.stdout.write("!")

Hello world!

Comments

0

Console.log doesn't allow you to print in the same line. However the task that you are trying to do can be done using the below:

console.log(lst.join(" "))

Output: 1 2 3 4 5

Comments

0

You could use Function#apply or spread syntax ....

var list = [1, 2, 3, 4, 5];

console.log.apply(null, list); // traditional
console.log(...list);          // ES6

Comments

0

The exact behavior of console.log is not specified by ECMAScript, so it varies from implementation to implementation, but it is intended to be used for logging events. The fact that each event usually becomes a single line in a web browser's console window led it to be the Node.js equivalent of "println", but outside the context of a specific implementation its behavior is not predictable.

You can pass multiple parameters to log, in which case most (but not all) implementations will print them all out on the same line separated by whitespace. So if you're using one of those implementations, and you have a list and just want all the elements on one line separated by space, you can use apply to call the method as if each element of the list was a separate argument:

console.log.apply(console, lst);

Caveat: if the first argument is a string that contains what look like Formatter format control sequences (%s, etc.), it will be parsed as such and the remaining arguments used to fill in those slots.

But the most reliable way to achieve the desired result is to build a single string yourself representing what you want the final line to look like, and then call console.log exactly once with that string as the argument.

1 Comment

The latter also won't print on a single line in Safari. In fact, it's probably sensible to say that (especially in browsers), console.log doesn't really give you much in the way of guarantees of what it will do with arbitrary (and multiple) arguments. If you want something to be on the same 'line', you should give it a string.
-1

You could do it easily by using the join() function which can be used on any array in JavaScript!

For example:

var lst = ["check","word","3","could","hello"]
console.log(lst.join())

source-code:
https://jsfiddle.net/Lpdurxsb/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.