1

I am trying to understand how to check which variable(s) are not defined in an array and how to do something with them. Unfortunately the code I have would overwrite variables which have already been set in the script.

Any help or suggestions would be appreciated.

for (var i = 0; i < result.length; i++) {      

    if(window['user' + i] == undefined){

       window['user' + i] = // DO SOMETHING WITH THE VARIABLE WHICH IS NOT DEFINED

    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AlexanderC
  • 13
  • 2
  • 3
    use `typeof(variable) == "undefined"` – Ravi Oct 17 '14 at 13:10
  • need a better explanation of issue and why you are using `window['user' + i]`. What is objective? – charlietfl Oct 17 '14 at 13:11
  • @Ravi what's wrong with his way? – Karl-André Gagnon Oct 17 '14 at 13:11
  • @Ravi Why wouldn't it? http://jsfiddle.net/a8wfg2pv/ – Karl-André Gagnon Oct 17 '14 at 13:16
  • Try this way http://stackoverflow.com/questions/2778901/javascript-undefined-compare – Pascal Oct 17 '14 at 13:19
  • _your script works_ @Karl-Andre-Gagnon tested it and I have tested it locally and I can't get it **not** to work. – somethinghere Oct 17 '14 at 13:20
  • @Karl-AndréGagnon The `typeof` operator is guaranteed to return a string. Direct comparisons against `undefined` are troublesome as undefined can be overwritten. `window.undefined = "omg"; "omg" == undefined // true`. But is ES5, maybe is worth mentioning that undefined is now described as non-writable, non-configurable and non-enumerable. So this will not be the case in modern browsers. – Ravi Oct 17 '14 at 13:21
  • @Ravi my code can check if a variable is set or not. My problem is that i don't know which variable is undefined since it's in an array. – AlexanderC Oct 17 '14 at 13:23
  • @Ravi I'm not saying typeof is bad, I do recommend it. But his code is fine. And you need to be really stupid to override the `window.undefined`.... `== undefined` is *good enough* when you test an object property, argument or array index. Testing a variable though will throw an error. – Karl-André Gagnon Oct 17 '14 at 13:23
  • @Karl-AndréGagnon I am not saying it cannot do that, but it's just conventional and effective to check by type for undefined values. That's all. – Ravi Oct 17 '14 at 13:27
  • @Ravi I wouldn't say conventional, since a lot of plugin use `=== undefined` for a slightly better performance. They initiate their plugin like that : `(function($, undefined){}).call(window, jQuery)`. But yeah, outside a closure like that, `typeof` is foolproof! – Karl-André Gagnon Oct 17 '14 at 13:31
  • @Karl-AndréGagnon ("since a lot of plugin use" == "conventional") to me :) . And yeah, `===` is more appropriate for type checking. – Ravi Oct 17 '14 at 13:36
  • 1
    @Ravi That's what I meant ;) `=== undefined` is not in quote so, i was saying `x === undefined` (no `typeof()`). Anyway, now this discussion is diverging from the OP and I think we understood each other! So, have a good day sir! – Karl-André Gagnon Oct 17 '14 at 13:39
  • @Karl-AndréGagnon Not a sir, i'm just a beginner, trying hard to make sense to others. :) – Ravi Oct 17 '14 at 13:44

2 Answers2

0

I believe this also works if your just you're just looking for any values that could be false as well

    if(!window['user' + i])
johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 1
    It will catch falsy value, maybe the OP can defined the variable to `false`, `0` or any other falsy value... – Karl-André Gagnon Oct 17 '14 at 13:28
  • What do you mean doesn't this catch undefined. example run this in your browser console. `var Temp = {}; if(!temp["test"]){console.log('this is not defined')}` this should log "this is not defined" – johnny 5 Oct 17 '14 at 13:31
  • 1
    I did not said it doesn't catch `undefined`, I said it catch falsy value. undefined is a falsy value, so it catch it. But 0 is also falsy. Test: `var temp = {x:0}; if(!temp["x"]){console.log('this is not defined')}`. OP wouldnt want to log to pop here. – Karl-André Gagnon Oct 17 '14 at 13:34
  • 1
    I just realized what you meant. I editted my response you can use this depending on the scenario. – johnny 5 Oct 17 '14 at 13:35
0

There are two types of undefined properties: those which have not been defined, and those which have been set to undefined.

If you need to detect properties which have not been defined on on object (or array), use Object.prototype.hasOwnProperty(), like so:

for (var i = 0; i < result.length; i++) {      
    if (!Object.prototype.hasOwnProperty.call(window, 'user' + i)) {
        window['user' + i] = // DO SOMETHING WITH THE VARIABLE WHICH IS NOT DEFINED
    }
}

Since window is an Object, you can also call hasOwnProperty directly through it, if you trust that nobody has set it (or Object.prototype.hasOwnProperty, which window inherits) to something else. The code for that would look like this:

for (var i = 0; i < result.length; i++) {      
    if (!window.hasOwnProperty('user' + i)) {
        window['user' + i] = // DO SOMETHING WITH THE VARIABLE WHICH IS NOT DEFINED
    }
}

If you need to specifically detect properties set to undefined, then start with the previous check and if true, check the value. Assuming that no one has changed window.hasOwnProperty, that would look like this:

for (var i = 0; i < result.length; i++) {      
    if (window.hasOwnProperty('user' + i) && window['user' + i] === undefined) {
        window['user' + i] = // DO SOMETHING WITH THE VARIABLE WHICH IS NOT DEFINED
    }
}

You may also need to check for properties that are undefined, or set to undefined or null. Again assuming that no one has changed window.hasOwnProperty, that might look like this:

for (var i = 0; i < result.length; i++) {      
    if (!window.hasOwnProperty('user' + i) || window['user' + i] === undefined || window['user' + i] === null) {
        window['user' + i] = // DO SOMETHING WITH THE VARIABLE WHICH IS NOT DEFINED
    }
}
The Spooniest
  • 2,863
  • 14
  • 14