Is there a standard function to check for null, un

2018-12-31 04:11发布

Is there a universal JavaScript function that checks that a variable has a value and ensures that it's not undefined or null? I've got this code, but I'm not sure if it covers all cases:

function isEmpty(val){
    return (val === undefined || val == null || val.length <= 0) ? true : false;
}

30条回答
高级女魔头
2楼-- · 2018-12-31 04:54
try{

     let vari = obj.propTest; // obj may be don't have propTest property

        ...
} catch(NullException){
    // do something here
}

I think using try catch will avoid any error of null check, also in Angular or JavaScript Just catching null exception and process in it.

查看更多
裙下三千臣
3楼-- · 2018-12-31 04:56

You can use bellow code for check all four(4) condition for validation like not null, not blank, not undefined and not zero only use this code (!(!(variable))) in javascript and jquery.

function myFunction() {
    var data;  //The Values can be like as null, blank, undefined, zero you can test

    if(!(!(data)))
    {
        alert("data "+data);
    } 
    else 
    {
        alert("data is "+data);
    }
}
查看更多
公子世无双
4楼-- · 2018-12-31 04:58

The verbose method to check if value is undefined or null is:

return value === undefined || value === null;

You can also use the == operator but this expects one to know all the rules:

return value == null; // also returns true if value is undefined
查看更多
长期被迫恋爱
5楼-- · 2018-12-31 04:58

For everyone coming here for having similar question, the following works great and I have it in my library the last years:

(function(g3, $, window, document, undefined){
   g3.utils = g3.utils || {};
/********************************Function type()********************************
* Returns a lowercase string representation of an object's constructor.
* @module {g3.utils}
* @function {g3.utils.type}
* @public
* @param {Type} 'obj' is any type native, host or custom.
* @return {String} Returns a lowercase string representing the object's 
* constructor which is different from word 'object' if they are not custom.
* @reference http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
* http://stackoverflow.com/questions/3215046/differentiating-between-arrays-and-hashes-in-javascript
* http://javascript.info/tutorial/type-detection
*******************************************************************************/
g3.utils.type = function (obj){
   if(obj === null)
      return 'null';
   else if(typeof obj === 'undefined')
      return 'undefined';
   return Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
};
}(window.g3 = window.g3 || {}, jQuery, window, document));
查看更多
柔情千种
6楼-- · 2018-12-31 04:59

I think using the ? operator is slightly cleaner.

var ? function_if_exists() : function_if_doesnt_exist();
查看更多
素衣白纱
7楼-- · 2018-12-31 04:59

I will leave a registered solution that I liked a lot:

First let's define that a blank variable is null, or undefined, or if it has length, it is zero, or if it is an object, it has no keys:

function isEmpty (value) {
  return (
    // null or undefined
    (value == null) ||

    // has length and it's zero
    (value.hasOwnProperty('length') && value.length === 0) ||

    // is an Object and has no keys
    (value.constructor === Object && Object.keys(value).length === 0)
  )
}

Returns:

  • true: undefined, null, "", [], {}
  • false: true, false, 1, 0, -1, "foo", [1, 2, 3], { foo: 1 }
查看更多
登录 后发表回答