1

My output right now is this: Output

My code is:

var a = [2, 4];
var b = [5, 6];

console.log(a, "\n", b);

I want the output to look like this:

[2, 4]
[5, 6]

4 Answers 4

2

Here's one way:

var a = [2, 4];

var b = [5, 6];

console.log('',a,'\n',b)

Sign up to request clarification or add additional context in comments.

7 Comments

It works, by the way there is a unwanted space before the output. Is there any way to remove this space? And how does this code work?
Within console.log, when you set the first argument as a string, it considers the format as a string. Meaning, arguments that follow the string will also be considered strings. - If you use any other format for the first argument then it won't output the way you expect because it won't assume the rest of your arguments as strings. When using '\n' string format is required.
Your solution is so much close to my expected output and I think that's the best solution for my expected output. So there's no way to remove the space before the output. Thanks for answer. Take love.
I have an idea to remove the space but I'm currently out for lunch. Will update when I'm back. :)
Wow, it would be great! I am waiting for reply.
|
2

I think you can use

var a = [2, 4];

var b = [5, 6];

console.log(a + "\n" + b);

It's working for me.

2 Comments

It returns output as string!
Okay, then you can write console.log twice: console.log(a); console.log(b);
1

You can use this:

console.log(`${a}\n${b}`);

1 Comment

It returns as string, i want the output as array.
0

Try using the array.toString() function.

var a = [2, 4];
var b = [5, 6];

console.log(a.toString() + '\n' + b.toString())

This code would return:

2,4
5,6

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.