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;
}
Dillinger
  • 1,823
  • 4
  • 33
  • 78
  • 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 – mikeyq6 Nov 18 '15 at 13:19
  • Maybe I could check first if lastFocusedEventData is defined or not. – Dillinger Nov 18 '15 at 13:20
  • You could do `if (appointment)` which will be true if appointment is not empty, null, undefined or 0 – mplungjan Nov 18 '15 at 13:21
  • have you tried check without quotes? –  Nov 18 '15 at 13:22
  • Must always use quotes when comparingt undefined types. See http://stackoverflow.com/questions/5663277/what-is-the-difference-between-undefined-and-undefined – mikeyq6 Nov 18 '15 at 13:23

4 Answers4

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;
}
P. Janowski
  • 306
  • 1
  • 6
  • the problem is on variable valorization not in the condition as I said – Dillinger Nov 18 '15 at 13:18
  • 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. – P. Janowski Nov 18 '15 at 13:32
  • the problem is on lastFocusedEventData that's undefined. See my updated code I got the same error. – Dillinger Nov 18 '15 at 13:35
  • Same principle applies if the lastFocusedEventData is undefined. You are trying to access data of undefined property which throws an error. – P. Janowski Nov 18 '15 at 13:51
  • if(typeof BackendCalendar.lastFocusedEventData !== 'undefined') { appointment = BackendCalendar.lastFocusedEventData.data; } //Remove the brackets inside an if statement – P. Janowski Nov 18 '15 at 13:58
0
if (typeof appointment != 'undefined') ...
wizzardmr42
  • 1,634
  • 12
  • 22
  • in that case you may need to check if BackendCalendar is defined and/or if BackendCalendar.lastFocusedEventData is defined first – wizzardmr42 Nov 18 '15 at 13:18
  • 1
    FYI http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – TayTay Nov 18 '15 at 13:27
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)

Shaun
  • 933
  • 9
  • 16
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

RiccardoC
  • 876
  • 5
  • 10