DEV Community

Cover image for JavaScript Interview Question #38: Can you add multiple arrays in JavaScript?
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com on

JavaScript Interview Question #38: Can you add multiple arrays in JavaScript?

js-test-38

Can you add multiple arrays in JavaScript? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

The function add(x, y, z) applies the + operator to the provided arguments. Or, simply put, adds them up.

In line 5 we provide it with 3 arrays.

Whenever you try to add arrays in JavaScript, they will be first converted to strings. Every element is separated from the next one by a comma and a single space. In our case:

  • 1, 2
  • 3, 4
  • 5, 6

Then these strings are concatenated, or “glued” together to make a result.


ANSWER: a string 1, 23, 45, 6 will be printed to the console.

Learn Full-Stack JavaScript

Top comments (8)

Collapse
 
anthager profile image
Anton

Ha! Always fun to see these things! But just as a heads up to people looking through this thread looking for advise for what to study before their interviews, stuff like this is not asked in interviews (atleast not in my experience) but there is a point of knowing that js sometimes does unexpected conversions. Just knowing that and answering "Im not sure but unexpected conversions might happen so I would try it out in the console" should be enough

Collapse
 
alphavader profile image
KB

This 100%

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Questions like this can tell the depth of the candidate's knowledge. I've seen "senior" devs completely fail and get pissed off with these "junior" questions.

Collapse
 
anthager profile image
Anton

That says something about their character and I definitely wouldnt want someone with that attitude in my team. But i could see why someone who would consider themselves senior getting frustrated when only asked trivia style questions and if an answer like "there might be fishy stuff going on here so I would just look it up" is not being accepted. Being senior is not having everything memorised, at least not in my book. But getting pissed of looks very bad indeed

Collapse
 
lucassperez profile image
Lucas Perez

This is one of those wtfjs moments

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Eventually, we all get used to it :)

Collapse
 
polyterative profile image
Vladyslav Yakovenko • Edited

this doesn't work on arrays of objects right?

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Arrays of objects will behave similarly and be converted to strings as well.