JavaScript null check

前端 未结 8 1011
小鲜肉
小鲜肉 2020-12-04 06:20

I\'ve come across the following code:

function test(data) {
    if (data != null && data !== undefined) {
        // some code here
    }
}
         


        
8条回答
  •  心在旅途
    2020-12-04 06:58

    An “undefined variable” is different from the value undefined.

    An undefined variable:

    var a;
    alert(b); // ReferenceError: b is not defined
    

    A variable with the value undefined:

    var a;
    alert(a); // Alerts “undefined”
    

    When a function takes an argument, that argument is always declared even if its value is undefined, and so there won’t be any error. You are right about != null followed by !== undefined being useless, though.

提交回复
热议问题