Is there a standard function to check for null, undefined, or blank variables in JavaScript?

前端 未结 30 3659
眼角桃花
眼角桃花 2020-11-21 23:37

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,

30条回答
  •  时光取名叫无心
    2020-11-22 00:15

    function isEmpty(val){
        return !val;
    }
    

    but this solution is over-engineered, if you dont'want to modify the function later for busines-model needings, then is cleaner to use it directly in code:

    if(!val)...
    

提交回复
热议问题