How do I check for undefined values in IE8?
internet-explorer-8, javascript, jquery, object
Solution
You can use this approach, but it would not reduce your code a lot:
filters.max_price = filters.max_price || 2000;
This, however, would overwrite the value if it's 0. The best approach remains:
if(typeof filters.max_price === 'undefined'){
// init default
}
Problem
I have this in my javascript: ``` console.log(filters); console.log('----'); console.log(filters.max_price); ``` In Chrome, it shows this. This is the expected behavior. ``` Object {max_price: undefined, sort_by: undefined, distance: undefined, start: undefined, num: undefined} ---- undefined ``` In IE8, the log shows this: ``` LOG: Object Object ---- LOG: String ``` Why does IE8 think it is a string? I need to know if it's undefined. I have lots of code that sets default values. ``` if(typeof filters.max_price == undefined){ //I use this technique a lot! filter.max_price = 2000; } ``` How can I check for undefine-ds in IE8? Should I do this? This seems to work (yay...), but it seems cheap and hacky. ``` if(!filters.max_price || typeof filters.max_price == 'undefined'){ ``` Is there a simple way I can do this with underscore?