How can I check whether a variable is defined in Node.js?

前端 未结 6 459
不知归路
不知归路 2021-01-31 01:18

I am working on a program in node.js which is actually js.

I have a variable :

var query = azure.TableQuery...

looks this line of the

6条回答
  •  执念已碎
    2021-01-31 02:01

    For easy tasks I often simply do it like:

    var undef;
    
    // Fails on undefined variables
    if (query !== undef) {
        // variable is defined
    } else {
        // else do this
    }
    

    Or if you simply want to check for a nulled value too..

    var undef;
    
    // Fails on undefined variables
    // And even fails on null values
    if (query != undef) {
        // variable is defined and not null
    } else {
        // else do this
    }
    

提交回复
热议问题