JavaScript Object Key Handling in Interviews

I recently faced a JavaScript interview, and the interviewer asked a small question that confused many candidates 🧠 let a = {} let b = { key: "b" } let c = { key: "c" } a[b] = 123 a[c] = 456 console.log(a[b]) The interviewer asked: “What will be the output?” Looks simple. But it tests a deep JavaScript concept. 🧠 What they were really testing: • How JavaScript handles object keys • Type coercion in objects • Understanding of implicit string conversion Many developers assume different objects create different keys. But JavaScript behaves differently. 🚀 Sometimes interviews are not about complex code. They are about understanding the language deeply. #JavaScript #FrontendInterview #MERNStack #WebDevelopment #CodingInterview #ProblemSolving #JavaScriptConcepts

Putting an obj as Key makes it [obj obj] in js objects,so doing a[b] gives { [object object]:123} Then doing a[c] again appends [object object] which overwrites the previous [obj obj] value nd stores 456. Then when u do a[b] it searches for a key=[object object] nd returns its value =456

This is such a clever way to test whether someone truly understands JavaScript's quirky behaviors. The string coercion with object keys catches so many people off guard, even experienced developers sometimes.

Like
Reply

The answer is 456, in object only string or symbol key allowed

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories