JavaScript Array and Object Coercion in Frontend Interviews

I asked this JavaScript question in a frontend interview… and many developers got it wrong. 👀 Question: What will be the output? console.log([] + []); Take a moment and think. Most developers expect an array as output. But the actual output is: "" Yes — an empty string. Why does this happen? In JavaScript, when we use the + operator with arrays, they are converted to strings first. [] → "" So internally JavaScript does this: "" + "" = "" That’s why the result is an empty string. Now it gets more interesting: console.log([] + {}); Output: "[object Object]" Because the object converts to a string representation. Why interviewers ask this They want to check your understanding of: Type coercion JavaScript internal conversions How the + operator works JavaScript can look simple… but its behavior can surprise even experienced developers. Frontend interviews don’t just test frameworks they test JavaScript fundamentals. #JavaScript #FrontendDevelopment #CodingInterview #WebDevelopment #Developers #Programming

This also highlights why many teams avoid implicit coercion and prefer explicit conversions. Using String([]) makes the intent clear, whereas [] + [] relies on JavaScript internals that can be confusing in production code.

This is the reason why Js is most hated language! 😅

See more comments

To view or add a comment, sign in

Explore content categories