Are there dictionaries in javascript like python?

16    Asked by jermai_8560 in Java , Asked on Apr 9, 2025

How does JavaScript handle key-value pairs like Python dictionaries? What built-in objects or structures can you use to store and access data in a similar way?

Answered by Kudo Taniguchi

Yes, JavaScript does have structures similar to Python dictionaries! While it doesn’t have a built-in “dictionary” type like Python, it offers a couple of ways to store key-value pairs effectively:

Plain JavaScript Objects ({})

 The most common way to create dictionary-like structures is by using objects.

const user = {
  name: "John",
  age: 30,
  city: "New York"
};
console.log(user["name"]); // Output: John
console.log(user.age); // Output: 30

 Objects in JavaScript allow string (and symbol) keys, and are widely used for this purpose.

Map Object

 If you need more advanced behavior (like using any data type as a key), JavaScript's Map is closer to Python's dictionary.

const myMap = new Map();
myMap.set("name", "Alice");
myMap.set(1, "one");
console.log(myMap.get("name")); // Output: Alice
console.log(myMap.get(1)); // Output: one

When to use Map over {}

 Use Map if:

  • You need to preserve insertion order
  • Keys might not be strings
  • Performance with frequent additions/deletions is important

In short, while JavaScript doesn’t have an exact “dictionary” like Python, objects and Map give you all the tools you need to work with key-value data. It's just a matter of choosing the right one based on your use case!



Your Answer

Interviews

Parent Categories