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

前端 未结 6 555
不知归路
不知归路 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 01:43

    If your variable is not declared nor defined:

    if ( typeof query !== 'undefined' ) { ... }
    

    If your variable is declared but undefined. (assuming the case here is that the variable might not be defined but it can be any other falsy value like false or "")

    if ( query ) { ... }
    

    If your variable is declared but can be undefined or null:

    if ( query != null ) { ... } // undefined == null
    

提交回复
热议问题