Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have undefined as a key when foreaching over a JavaScript object?

I have been doing a code review and I see code like this:

var a = { ... }; // in reality, an actual object filled with key-value pairs
for (var key in a) {
    if (!angular.isUndefined(key)) { // does this ever fail?
        do.stuff();
    }
}

My question is whether key can ever be undefined (or evaluated to true per angular.isUndefined). It seems unlikely, as when I try to do the following:

var a = { undefined: 'hello' }

It turns out that undefined is actually a String.

like image 625
geoff Avatar asked Nov 28 '25 23:11

geoff


1 Answers

My question is whether key can ever be undefined

No. All keys (property names) of an object are converted to strings.

like image 160
Felix Kling Avatar answered Dec 01 '25 12:12

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!