Sets and Maps
Sets and Maps are built-in data structures introduced in JavaScript that provide efficient ways to store unique values and key-value pairs, respectively.
// Set example
const mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(3);
console.log(mySet.has(2)); // Output: true
console.log(mySet.size); // Output: 3
// Map example
const myMap = new Map();
myMap.set('name', 'John');
myMap.set('age', 30);
console.log(myMap.get('name')); // Output: John
console.log(myMap.size); // Output: 2
Sets store unique values, allowing you to add, check for existence, and retrieve elements efficiently. Maps store key-value pairs and provide methods to set, get, and delete values based on the keys.
No comments:
Post a Comment