0

In my code I've a method that check if a select option is clicked or not, like this:

$("#selected-service").click(function ()

now all working fine, but inside of this method I valorize this variable:

var appointment = BackendCalendar.lastFocusedEventData.data;

in some case this variable is returned undefined, and this is normal 'cause this variable rapresent if the user is in the edit mode or adding mode of an appointment. In the first case the variable was returned undefined as well. Anyway, I perform this condition:

try
{
   var appointment = BackendCalendar.lastFocusedEventData.data;

   if (appointment != 'undefined')
   {
      //do this...
   }
   else 
   {
      //do this...
   }
}
catch(Ex){  console.log("Error=>" , Ex);    }

but the problem is that the else condition is never fire 'cause the code go in the catch exception. Now, the question is simple: how I can bring in the else if the variable is undefined?

POSSIBLE SOLUTION:

if(typeof(BackendCalendar.lastFocusedEventData !== 'undefined'))
{
    appointment = BackendCalendar.lastFocusedEventData.data;
}
5
  • What is the error received? Could the problem be because BackendCalendar or lastFocusedEventData are undefined or null? Also, you should use !== for comparing to undefined. Use quotes in the comparison if you are looking at the type. Ie. (typeof appointment !== "undefined") is ok Commented Nov 18, 2015 at 13:19
  • Maybe I could check first if lastFocusedEventData is defined or not. Commented Nov 18, 2015 at 13:20
  • You could do if (appointment) which will be true if appointment is not empty, null, undefined or 0 Commented Nov 18, 2015 at 13:21
  • have you tried check without quotes? Commented Nov 18, 2015 at 13:22
  • Must always use quotes when comparingt undefined types. See stackoverflow.com/questions/5663277/… Commented Nov 18, 2015 at 13:23

4 Answers 4

1

Try this, rather than checking content of the variable, check its type.

if(typeof appointment !== "undefined"){
//do this
} else {
//do that
}

EDIT:

This will work but remove the brackets:

if(typeof BackendCalendar.lastFocusedEventData !== 'undefined')
{
    appointment = BackendCalendar.lastFocusedEventData.data;
}
Sign up to request clarification or add additional context in comments.

5 Comments

the problem is on variable valorization not in the condition as I said
It must be because either BackendCalendar or lastFocusedEventData property are null or undefined. Lets say BackendCalendar is undefined, if you try to access property lastFocusedEventData of undefined it will throw an exception rather than returning undefined result. Hope this helps.
the problem is on lastFocusedEventData that's undefined. See my updated code I got the same error.
Same principle applies if the lastFocusedEventData is undefined. You are trying to access data of undefined property which throws an error.
if(typeof BackendCalendar.lastFocusedEventData !== 'undefined') { appointment = BackendCalendar.lastFocusedEventData.data; } //Remove the brackets inside an if statement
0
if (typeof appointment != 'undefined') ...

2 Comments

in that case you may need to check if BackendCalendar is defined and/or if BackendCalendar.lastFocusedEventData is defined first
0

Clearly the problem isn't with the appointment variable, it's with the:

BackendCalendar.lastFocusedEventData

Probably being null or undefined.

If you set appointment like so:

   var lastDate = BackendCalendar.lastFocusedEventData, appointment = lastDate ? lastDate.data : undefined

It should work.

Also personally I just use

if(!appointment) {
...

Which covers both null and undefined checks (if your sure it will NEVER be zero)

Comments

0

I created a function to verify all the object instance

function definedVar( string, containerVar ) {
    var splitted = string.split( "." );
    var testVar = containerVar;
    for( var i = 0; i < splitted.length; i++ ) {
        var propertyName = splitted[ i ];
        if( testVar[ propertyName ] == undefined ) {
            return false;
        }
        testVar = testVar[ propertyName ];
    }
    return true;
}

See it in action HERE

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.