Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

typescript

What is map.get() method in TypeScript?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

We use the get() method of a map in TypeScript to get the value for a key in a map. A map is a key-value pair data structure.

Syntax

Map.get(key)
The syntax for the get() method of a map in TypeScript

Parameters

Map: This is the map whose key's value we want to get.

key: This is the key whose value we want to get.

Return value

If the specified key exists, then the value is returned. Otherwise, undefined is returned.

Example

// create some Maps
let evenNumbers = new Map<string, number>([["two", 2], ["four", 4], ["eight", 8]])
let cart = new Map<string, number>([["rice", 500], ["bag", 10]])
let countries = new Map<string, string>([["NG", "Nigeria"], ["BR", "Brazil"], ["IN", "India"]])
let isMarried = new Map<string, boolean>([["James", false], ["Jane", true], ["Doe", false]])
let emptyMap = new Map<null, null>()
// get some values
console.log(evenNumbers.get("two")) // 2
console.log(cart.get("books")) // undefined
console.log(countries.get("IN")) // India
console.log(isMarried.get("James")) // false
console.log(emptyMap.get(null)) // undefined
The map.get() method in TypeScript

Explanation

  • Lines 2–6: We create some maps.
  • Lines 9–13: We use the get method to get the values of the keys specified for each map. Then, we log the results to the console.

RELATED TAGS

typescript

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring