How to check 'undefined' value in jQuery

JavascriptJqueryUndefined

Javascript Problem Overview


> Possible Duplicate:
> Detecting an undefined object property in JavaScript
> javascript undefined compare

How we can add a check for an undefined variable, like:

function A(val) {
  if (val == undefined) 
    // do this
  else
    // do this
}

Javascript Solutions


Solution 1 - Javascript

JQuery library was developed specifically to simplify and to unify certain JavaScript functionality.

However if you need to check a variable against undefined value, there is no need to invent any special method, since JavaScript has a typeof operator, which is simple, fast and cross-platform:

if (typeof value === "undefined") {
    // ...
}

It returns a string indicating the type of the variable or other unevaluated operand. The main advantage of this method, compared to if (value === undefined) { ... }, is that typeof will never raise an exception in case if variable value does not exist.

Solution 2 - Javascript

In this case you can use a === undefined comparison: if(val === undefined)

This works because val always exists (it's a function argument).

If you wanted to test an arbitrary variable that is not an argument, i.e. might not be defined at all, you'd have to use if(typeof val === 'undefined') to avoid an exception in case val didn't exist.

Solution 3 - Javascript

Note that typeof always returns a string, and doesn't generate an error if the variable doesn't exist at all.

function A(val){
  if(typeof(val)  === "undefined") 
    //do this
  else
   //do this
}

Solution 4 - Javascript

I know I am late to answer the function but jquery have a in build function to do this

if(jQuery.type(val) === "undefined"){
    //Some code goes here
}

Refer jquery API document of jquery.type https://api.jquery.com/jQuery.type/ for the same.

Solution 5 - Javascript

You can use shorthand technique to check whether it is undefined or null

 function A(val)
 {
   if(val || "") 
   //do this
 else
 //do this
 }

hope this will help you

Solution 6 - Javascript

If you have names of the element and not id we can achieve the undefined check on all text elements (for example) as below and fill them with a default value say 0.0:

var aFieldsCannotBeNull=['ast_chkacc_bwr','ast_savacc_bwr'];
 jQuery.each(aFieldsCannotBeNull,function(nShowIndex,sShowKey) {
   var $_oField = jQuery("input[name='"+sShowKey+"']");
   if($_oField.val().trim().length === 0){
       $_oField.val('0.0')
    }
  })

Solution 7 - Javascript

when I am testing "typeof obj === undefined", the alert(typeof obj) returning object, even though obj is undefined. Since obj is type of Object its returning Object, not undefined.

So after hours of testing I opted below technique.

if(document.getElementById(obj) !== null){
//do...
}else{
//do...
}

I am not sure why the first technique didn't work.But I get done my work using this.

Solution 8 - Javascript

I am not sure it is the best solution, but it works fine:

if($someObject['length']!=0){
    //do someting
}

Solution 9 - Javascript

function isValue(value, def, is_return) {
    if ( $.type(value) == 'null'
        || $.type(value) == 'undefined'
        || $.trim(value) == ''
        || ($.type(value) == 'number' && !$.isNumeric(value))
        || ($.type(value) == 'array' && value.length == 0)
        || ($.type(value) == 'object' && $.isEmptyObject(value)) ) {
        return ($.type(def) != 'undefined') ? def : false;
    } else {
        return ($.type(is_return) == 'boolean' && is_return === true ? value : true);
    }
}

try this~ all type checker

Solution 10 - Javascript

Check if undefined or not

if(typeof myVal === "undefined") {
   //some code
}

Check if undefined or null or empty or false or 0

if(!myVal) {
   // some code
} else {
   // myVal is flawless 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGauravView Question on Stackoverflow
Solution 1 - JavascriptVisioNView Answer on Stackoverflow
Solution 2 - JavascriptThiefMasterView Answer on Stackoverflow
Solution 3 - JavascriptJignesh RajputView Answer on Stackoverflow
Solution 4 - JavascriptArpitaView Answer on Stackoverflow
Solution 5 - JavascriptRohidas KadamView Answer on Stackoverflow
Solution 6 - JavascriptYoosaf AbdullaView Answer on Stackoverflow
Solution 7 - JavascriptSridhar KView Answer on Stackoverflow
Solution 8 - JavascriptesduView Answer on Stackoverflow
Solution 9 - JavascriptMGKView Answer on Stackoverflow
Solution 10 - Javascriptburak isikView Answer on Stackoverflow