Interview Question - JavaScript Map - Delete

This is an interview question on JavaScript Map. You will make use of delete method on Map to find the output of the code

Code

let mapping = new Map([[1, 2], [2, 3], [3, 4]])
mapping.delete()
console.log(mapping)

Full Transcript

00:01 In this question, we are creating a new map from a two dimensional array. Then, we call the delete method on this newly created map. Finally, we are printing the value of map on the console. Let's work together to find the right answer. In the first line we create a map with entries taken from 2D array. Each entry represents a key value pair. Once the map is created, the variable mapping contains the entry as we see here. In the next line we call the method called delete on the mapping variable. The delete method takes the key of the entry and deletes the corresponding entry if the key is found. Since we did not pass any key, no deletions will be made. Therefore, when the next line executes, it prints the entire map on the console, which is the correct answer. Let's run it on the editor to make sure that our understanding is correct.

00:50 As we see when I hit run for this code, we get the entire map back as the answer. And that's it. If you work with JavaScript or any other frameworks such as jQuery, angular, or react, you have executed JavaScript on the browser. So the new course that we have launched is relevant for you. It's called Browser JavaScript and event loop. This goes educates on how JavaScript is executed behind the scenes, and we'll cover various topics such as ExecutionContext and event loop. You will have the opportunity to visually see every step of JavaScript code execution. Check out the course link in the description below to learn more about the course and let us know if you have any questions. Keep learning and see you next time.