The usual way to check if the value of a property is the special value undefined
, is:
if(o.myProperty === undefined) {
alert("myProperty value is the special value `undefined`");
}
To check if an object does not actually have such a property, and will therefore return undefined
by default when you try and access it:
if(!o.hasOwnProperty('myProperty')) {
alert("myProperty does not exist");
}
To check if the value associated with an identifier is the special value undefined
, or if that identifier has not been declared. Note: this method is the only way of referring to an undeclared (note: different from having a value of undefined
) identifier without an early error:
if(typeof myVariable === 'undefined') {
alert('myVariable is either the special value `undefined`, or it has not been declared');
}
In versions of JavaScript prior to ECMAScript 5, the property named "undefined" on the global object was writeable, and therefore a simple check foo === undefined
might behave unexpectedly if it had accidentally been redefined. In modern JavaScript, the property is read-only.
However, in modern JavaScript, "undefined" is not a keyword, and so variables inside functions can be named "undefined" and shadow the global property.
If you are worried about this (unlikely) edge case, you can use the void operator to get at the special undefined
value itself:
if(myVariable === void 0) {
alert("myVariable is the special value `undefined`");
}
If you just want to check whether there's a truthy value, you can do:
if (strValue) {
//do something
}
If you need to check specifically for an empty string over null, I would think checking against ""
is your best bet, using the ===
operator (so that you know that it is, in fact, a string you're comparing against).
if (strValue === "") {
//...
}
Best Answer
What you are seeing is not the return value of
require(...)
, simply because that's not what you have typed.You are observing the result of the statement,
var aws = require('aws-sdk')
. And that statement, a variable declaration with an assignment, has an "undefined value". If you inspect what has been stored in theaws
variable, you'll see that it is not undefined, it contains the module returned by therequire(...)
call.Try this:
var x = 2
You'll also see
undefined
. And you know that "2" is definitely not "undefined".Now, try this:
require('aws-sdk')
(or any other module, such ashttp
; note that this is just requiring the module, not assigning it to any variable)You'll see the module being printed in the REPL.
Finally, try this:
var aws = require('aws-sdk')
aws
This will print the value of the
aws
variable into the REPL. And that value is whatever has been returned by therequire(...)
call. And you'll see that it's definitely not "undefined".This is the precisely expected behavior of Node.js in whatever platform (i.e., what you are observing is completely unrelated to the fact that you are running Node on AWS; you could run it on your laptop, whatever OS you have, and you'd see the exact same behavior).