JavaScript Map get() method - JavaScript Tutorial

JavaScript Map get() method

BLUF: This tutorial on JavaScript Map get() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Map get() method

Understanding JavaScript Map get() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The get method of the JavaScript Map object retrieves the value associated with a given key from that Map instance.

Syntax

The get function can be expressed using the syntax outlined below:

Example

mapObj.get(key)

Parameter

key - The key to be searched.

Return

The value of specified key.

JavaScript Map get method example

In this section, we will explore the get method by examining several examples.

Example 1

Let's see a simple example of get method.

Example

<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:

Output

jQuery

AngularJS

Bootstrap

Example 2

Let’s examine a scenario to ascertain the outcome when a map object does not include the designated key element.

Example

<script>

var map=new Map();

map.set(1,"jQuery");

map.set(2,"AngularJS");

map.set(3,"Bootstrap");



document.writeln(map.get(5)); //returns undefined

</script>

Output:

Output

undefined

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience