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

前端 未结 6 554
不知归路
不知归路 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:58

    It sounds like you're doing property checking on an object! If you want to check a property exists (but can be values such as null or 0 in addition to truthy values), the in operator can make for some nice syntax.

    var foo = { bar: 1234, baz: null };
    console.log("bar in foo:", "bar" in foo); // true
    console.log("baz in foo:", "baz" in foo); // true
    console.log("otherProp in foo:", "otherProp" in foo) // false
    console.log("__proto__ in foo:", "__proto__" in foo) // true
    

    As you can see, the __proto__ property is going to be thrown here. This is true for all inherited properties. For further reading, I'd recommend the MDN page:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

提交回复
热议问题