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

前端 未结 30 4060
眼角桃花
眼角桃花 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:09

    The first answer with best rating is wrong. If value is undefined it will throw an exception in modern browsers. You have to use:

    if (typeof(value) !== "undefined" && value)
    

    or

    if (typeof value  !== "undefined" && value)
    

提交回复
热议问题