Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Right - should be using set and get - was written out without running it.

But isn't this still a map that has float and integer keys? Why doesn't it count?



from what I got, it's a very stupid interaction of different javascript language features.

Every object in JS has basic map functionality. Since version 1 of the language, you could always write things like:

  var o = new Object();
  o["foo"] = "bar";
  o["foo"] // --> "bar"
However, this functionality is relatively limited: It only supports strings (and symbols) as keys and it interacts badly with other methods and properties which are part of the object. Hence why an explicit "Map" type was added to the language much later.

The problem is, Maps are also objects, so they still inherit the "old" map functionality in addition to the new functionality. Those two systems are completely independent, even though they act on the same object. So writing

  mymap["foo"] = 1
and

  mymap.set("foo", 1)
both store the mapping "foo" -> 1, but in completely different places. Only the second one will actually put it into the store of the map, while the first one will just add it as a generic object property.

You can see it when trying to retrieve the value again:

  mymap["foo"] = 1
  mymap["foo"]; // --> 1
  mymap.get("foo"); // --> undefined
likewise:

  mymap.set("bar", 1);
  mymap.get("bar"); // --> 1
  mymap["bar"]; // --> undefined


Yes the square brackets were just a typo. But maps do support float keys - you can try it yourself!

  map = new Map();
  map.set(1.2, 3.4);
  log(typeof(map.keys().next().value));
That says the key is a number, and it's a floating point number. So what did they mean by "ECMAScript does not have maps where keys can be integers and neither can keys be floats"?


Ah, I misunderstood that, sorry.

I think the GP was wrong there. JS maps absolutely do support float keys, it's just generally a bad idea to use them if you don't restrict yourself to integers.


Object property names are always strings, even though you can set them via number literals (as the person you replied to said):

    const object = {}
    object[1] = 3

    console.log(object["1"]) // 3
    for (const key in object) {
      console.log(typeof key) // "string"
    }
This is different from keys of the Map data structure, which are actually able to be any type of value (even silly stuff like other Maps).


I don't know - I think that proves what I said - map keys can be floats.


For sure, I never disagreed with that (I even said "JavaScript maps absolutely can use numbers (or any type) as keys" in my other comment).

But object property names cannot be floats (which is what your example was, despite them being properties of a Map object).




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: