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
.
Top comments (6)
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.
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.
Wait, there are languages where using the
+
operation concatenates the arrays?Python :v
And you can do this in Elixir too, but with
++
And in some languages, you can overload the
+
operator and write your own implementation of it that will behave differently for arrays/objects/etc.Python