Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Tried:
var xxx = (typeof my_var.property !== 'undefined') ? my_var.property : 'fu';
I get:
Uncaught exception: ReferenceError: Undefined variable: my_var
well I know it's undefined, but why do I get that error?? xxx should take the fu value...
Your code checks if the type of my_var.property is undefined. But that can't be checked because the type of my_var itself is already undefined.
my_var.property
my_var
Add a comment
Try to check only my_var first, it can be undefined, too
undefined
var xxx = (typeof my_var !== 'undefined' && my_var.property !== 'undefined') ? my_var.property : 'fu';
Evaluation of my_var.property fails because my_var is null or undefined. Enhance your code like this:
var xxx = (my_var && typeof my_var.property !== 'undefined') ? my_var.property : 'fu';
Add another check for my_var
var xxx = (typeof my_var != 'undefined' && typeof my_var.property !== 'undefined')? my_var.property : 'fu';
i think you should first check for my_var
if(!myvar) { var xxx = (typeof my_var.property !== 'undefined') ? my_var.property : 'fu'; alert(xxx); }
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.