C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Map set() methodThe JavaScript map set() method is used to add or updates an element to map object with the particular key-value pair. Each value must have a unique key. SyntaxThe set() method is represented by the following syntax: mapObj.set(key,value) Parameterkey - It represents the key to be added. value - It represents the value to be added. ReturnThe Map object. JavaScript Map set() method exampleHere, we will understand set() method through various examples. Example 1Let's see an example to add key and value pair of elements to map object. <script> var map=new Map(); map.set(1,"jQuery"); map.set(2,"AngularJS"); map.set(3,"Bootstrap"); document.writeln(map.get(1)+"<br>"); document.writeln(map.get(2)+"<br>"); document.writeln(map.get(3)); </script> Output: jQuery AngularJS Bootstrap Example 2In this example, we will determine the result when same key is added with different values. <script> var map=new Map(); map.set(1,"jQuery"); map.set(1,"AngularJS"); map.set(3,"Bootstrap"); document.writeln(map.get(1)+"<br>"); document.writeln(map.get(2)+"<br>"); document.writeln(map.get(3)); </script> Output: AngularJS undefined Bootstrap
Next TopicJavaScript Map
|