0

In a situation where an object property is retrieved through a variable i.e.:

myObject[someField]

there is the possibility of someField (which is a string) to be undefined (the result of a potentially uninitialized string value). My experiments show that for all types of objects I can think of the result is undefined i.e.:

anyObject[undefined] === undefined

Is this a well known behavior, something I can rely on? Can't seem to find something in related documentation and my alternative would be to re-write the above as

someField ? myObject[someField] : undefined;

but would really prefer the succinct way if there is a guarantee that undefined is returned whenever we try to access the property undefined.

user3840170
  • 26,597
  • 4
  • 30
  • 62
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
  • `let a = { undefined: true }` and then `a[undefined]` is not that, but it's basically equivalent to `a.undefined`. All keys are "stringified". – tadman Aug 04 '23 at 14:17
  • 2
    The result of `String(undefined)` is the string value `"undefined"`. That's the actual name of the property your code is looking up when `someField` is `undefined`. – Pointy Aug 04 '23 at 14:17
  • @tadman I guess that settles it. Didn't know you can add an undefined key ... – Lorah Attkins Aug 04 '23 at 14:20
  • 2
    It's not an undefined key, it's a key with the label `"undefined"`. – tadman Aug 04 '23 at 14:20
  • But I don't have a label "undefined". I have the equivalent of `let someField; myObject[someField]` . Essentially I want to use a potentially uninitialized object to index a class instance – Lorah Attkins Aug 04 '23 at 14:22
  • 1
    @LorahAttkins if you write `x[undefined]`, JS will coerce `undefined` to a string first, which is `"undefined"`. See [the MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#property_names) and maybe we can clean up these comments once we're all on the same page – jcalz Aug 04 '23 at 14:25
  • I've untagged "TypeScript". If you try this in TypeScript you will get an error that `Type 'undefined' cannot be used as an index type.(2538)`, which you are apparently not asking about. So either this isn't about TypeScript, or it is but the answer is "don't even try to do this because [it's not valid](https://stackoverflow.com/q/41750390/2887218)" – jcalz Aug 04 '23 at 14:29

1 Answers1

3

No, accessing obj[undefined] will not always return undefined. Like any value used as property name, undefined will get coerced to a string (unless it is a symbol), so it will actually access a property named "undefined". obj[undefined] is equivalent to obj["undefined"] or obj.undefined. It will return the property value if such a property exists, e.g. when obj = {undefined: true};.

You should indeed write

someField != null ? myObject[someField] : undefined;

if someField: undefined | string.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I wonder if `myObject.someField` should work (docs and local experiments seem to agree on that) but best to stick with the more idiomatic way , thanks – Lorah Attkins Aug 04 '23 at 15:08
  • @LorahAttkins No, [that's something completely different](https://stackoverflow.com/q/4968406/1048572) – Bergi Aug 04 '23 at 15:27