DEV Community

loading...
Cover image for JavaScript Interview Question #33: Add two empty arrays and check the type

JavaScript Interview Question #33: Add two empty arrays and check the type

Coderslang: Become a Software Engineer
Teaching you to code at js.coderslang.com - JavaScript, HTML, CSS, Node.js, React.js, React Native
Originally published at learn.coderslang.com on ・1 min read

js-test-33

Array? Object? Undefined? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JavaScript, the + operator doesn’t do the concatenation of arrays.

Instead, it transforms them into strings and then does the string concatenation.

Two empty arrays become two empty strings, and their sum unsurprisingly is still an empty string.

What matters to us is the typeof that will return string in our case.


ANSWER: the output will be string.

Learn Full-Stack JavaScript

Discussion (6)

Collapse
joshuajarman profile image
Joshua Jarman • Edited

Bad Interview Question.

what do they even mean by "adding" arrays?
they can't ask bad misleading questions, that's a very hostile anti-pattern for interviewing.

trying to get a dev to express they understand javascript concatenation intricacies is not going to happen when the framer of the question indicates in the question that they aren't even clear enough to ask for that distinction properly.

they can't make the same mistake in the questions language that is the "gotcha" of the code problem.

🤦‍♂️ i see these types of hostile broken interview questions all the time and i always recommend Jr.s walk away from those companies. Clear indicator of major problems in how devs are evaluated and supported, imho.

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

Anti-pattern or not, people that don't know how the type coercion works in JS will certainly introduce a lot of bugs. That's why these questions are asked on pretty much every Junior interview.

P.S. The title could be better, I agree.

Collapse
polaroidkidd profile image
Daniel Einars • Edited

Wait, there are languages where using the + operation concatenates the arrays?

Collapse
pliavi profile image
Vitor "Pliavi" Silvério

Python :v
And you can do this in Elixir too, but with ++

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

And in some languages, you can overload the + operator and write your own implementation of it that will behave differently for arrays/objects/etc.

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

Python