FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

Β© 2026 FlareStart. All rights reserved.

Back to articles
Understanding Object Keys in JavaScript (Interview Trap Question)
How-ToWeb Development

Understanding Object Keys in JavaScript (Interview Trap Question)

via Dev.to JavaScriptKush Bhandari3h ago

I was recently asked an interesting JavaScript question in an interview: let obj = {}; let a = { a : 10 }; let b = { b : 20 }; obj [ a ] = 10 ; obj [ b ] = 20 ; console . log ( obj [ a ]); πŸ€” What will be the output? πŸ‘‰ Output: 20 πŸ” Why does this happen? In JavaScript, object keys can only be of type string or symbol . When you use an object as a key: obj [ a ] = 10 ; JavaScript internally converts the key to a string using .toString() : a . toString () // "[object Object]" b . toString () // "[object Object]" So effectively, your code becomes: obj [ " [object Object] " ] = 10 ; obj [ " [object Object] " ] = 20 ; πŸ‘‰ The second assignment overwrites the first one. So: console . log ( obj [ a ]); // 20 πŸ“Œ Follow-up Question 1: How to fix this issue? βœ… Option 1: Use Map (Recommended) If you want to use objects as keys, use Map : const map = new Map (); map . set ( a , 10 ); map . set ( b , 20 ); console . log ( map . get ( a )); // 10 console . log ( map . get ( b )); // 20 πŸ‘‰ Map preserves re

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
0 views

Related Articles

What Makes a Good Open Source PR (Lessons From Getting Mine Closed)
How-To

What Makes a Good Open Source PR (Lessons From Getting Mine Closed)

Dev.to β€’ 28m ago

Hoto’s powerful PixelDrive electric screwdriver is 25 percent off
How-To

Hoto’s powerful PixelDrive electric screwdriver is 25 percent off

The Verge β€’ 50m ago

How I turned my Pixel phone into a genuinely productive desktop computer - for free
How-To

How I turned my Pixel phone into a genuinely productive desktop computer - for free

ZDNet β€’ 2h ago

the world is your oyster - you can just do things.
How-To

the world is your oyster - you can just do things.

Medium Programming β€’ 2h ago

The Sonos Bluetooth Speaker Is $40 Off
How-To

The Sonos Bluetooth Speaker Is $40 Off

Wired β€’ 3h ago

Discover More Articles