1

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...

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Try to check only my_var first, it can be undefined, too

var xxx = (typeof my_var !== 'undefined' && my_var.property !== 'undefined') ? my_var.property : 'fu';

Comments

2

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';

Comments

2

Add another check for my_var

var xxx =  (typeof my_var != 'undefined' && typeof my_var.property !== 'undefined')? my_var.property : 'fu';

Comments

1

i think you should first check for my_var

if(!myvar) 
{
     var xxx = (typeof my_var.property !== 'undefined') ? my_var.property : 'fu';
     alert(xxx);
}

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.