how to compare a fbjs variable with fbjs undefined?

85 views Asked by At

i have an alert in fbjs which returns undefined but when i am comparing a fbjs variable with underined it does not work...

var params = document.getElementById('ques_form').serialize();
    user_answer = params.radio;
    if(user_answer=="undefined")
    {
        alert("please select any option");
        return false;
    }
2

There are 2 answers

0
AlfaTeK On BEST ANSWER
if(user_answer == undefined)

or more correctly:

if(user_answer === undefined)

undefined is a "special" value in javascript, not the string with value "undefined". When you compare to "undefined" you are comparing if that variable is a string with the text "undefined" instead of checking for the special undefined value in javascript.

Learn: http://www.w3schools.com/jsref/jsref_undefined.asp http://javascript.about.com/od/reference/g/sundefined.htm

0
Aizaz Barki On

I have found the solution of the problem as

if(first === undefined)   
   alert('First is undefined'); 
else   
   alert('First is defined');