What is the data type of a key in Map? [SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the data type of a key in Map? [SOLVED]

https://code.sololearn.com/WzUsmH42TrPa/?ref=app So Map allows us to define its keys in any data type. In this code we set two keys: name and "name". Both keys exist in Map and return their respective values. But what is the data type of the first key? It cannot be string type otherwise that would mean we set the same key again with different value. But it's not a variable name either right? Since we didn't define such variable anywhere in the code.

17th Nov 2021, 2:17 PM
Artur
Artur - avatar
6 Answers
+ 1
Ok, after some research I found out that 'name' is actually the variable in global object 'window'. And its value is... undefined? WHAT IN THE WORLD is that undefined variable with such a common name doing in global object? I have no idea xD. But whatever, that's why the 'name' variable in Map key gets converted to empty string "" from undefined.
17th Nov 2021, 11:27 PM
Artur
Artur - avatar
+ 1
And interestingly enough in Node.js code playground it throws referenceError as I expected https://code.sololearn.com/c6dtgzC73rg0/?ref=app
17th Nov 2021, 2:33 PM
Artur
Artur - avatar
+ 1
Artur, So JS Map is different with Node Map? why this different behaviour?
18th Nov 2021, 2:25 AM
Ipang
+ 1
Ipang No, it's not. Map stays the same anywhere. But Node.js itself is the different environment. In browser environment global object is 'window' whereas in Node it's 'global'. They have different properties and methods attached to them. Thus, browsers 'window' global object has 'name' property, but Nodes 'global' global object doesn't.
18th Nov 2021, 5:20 AM
Artur
Artur - avatar
+ 1
Oh okay, still learning these two ... Thanks for sharing the findings bro 👍
18th Nov 2021, 5:59 AM
Ipang
+ 1
Ipang you're welcome. Glad if I can share at least tiny knowledge🤗
18th Nov 2021, 6:23 AM
Artur
Artur - avatar