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?
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.
Try it. function test(t) { if (t === undefined) { return 'Undefined value! '; } return t; } let x; console. log(test(x)); // expected output: "Undefined value!"
We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.
undefined , Function , and Symbol values are not valid JSON values.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With