1

What is Map Coercion in JavaScript ?

 7 months ago
source link: https://www.geeksforgeeks.org/what-is-map-coercion-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What is Map Coercion in JavaScript ?

Courses

In JavaScript, Map coercion refers to the automatic conversion of an object or an iterable (such as an array) into a Map using the Map constructor. This coercion happens implicitly when an object or iterable with key-value pairs is provided as an argument to the Map constructor.

Example: Here, the array keyValuePairs contains nested arrays representing key-value pairs. When this array is passed as an argument to the Map constructor, it is coerced into a Map, and the resulting Map (myMap) contains the key-value pairs from the array.

  • Javascript
const keyValuePairs = [
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3']
];
// Map coercion using the Map constructor
const myMap = new Map(keyValuePairs);
console.log(myMap);
Output
Map(3) { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!

Three 90 Challenge ending on 5th Feb! Last chance to get 90% refund by completing 90% course in 90 days. Explore offer now.
Last Updated : 02 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK