Question

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;
    }
Was it helpful?

Solution

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

OTHER TIPS

I have found the solution of the problem as

if(first === undefined)   
   alert('First is undefined'); 
else   
   alert('First is defined');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top