JavaScript null check

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

I\'ve come across the following code:

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


        
8条回答
  •  萌比男神i
    2020-12-04 06:55

    In your case use data==null (which is true ONLY for null and undefined - on second picture focus on rows/columns null-undefined)

    function test(data) {
        if (data != null) {
            console.log('Data: ', data);
        }
    }
    
    test();          // the data=undefined
    test(null);      // the data=null
    test(undefined); // the data=undefined
    
    test(0); 
    test(false); 
    test('something');

    Here you have all (src):

    if

    == (its negation !=)

    === (its negation !==)

提交回复
热议问题