I’m a bit confused about JavaScript’s undefined and null values.
What does if (!testvar) actually do? Does it test for undefined and null or just undefined?
Once a variable is defined can I clear it back to undefined (therefore deleting the variable)?
Can I pass undefined as a parameter? E.g.:
function test(var1, var2, var3) {
}
test("value1", undefined, "value2");
nullgenerally behaves similarly to other scripting languages’ concepts of the out-of-band ‘null’, ‘nil’ or ‘None’ objects.undefined, on the other hand, is a weird JavaScript quirk. It’s a singleton object that represents out-of-band values, essentially a second similar-but-differentnull. It comes up:When you call a function with fewer arguments than the arguments list in the
functionstatement lists, the unpassed arguments are set toundefined. You can test for that with eg.:With this method you can’t tell the difference between
dosomething(1)anddosomething(1, undefined);arg2will be the same value in both. If you need to tell the difference you can look atarguments.length, but doing optional arguments like that isn’t generally very readable.When a function has no
return value;, it returnsundefined. There’s generally no need to use such a return result.When you declare a variable by having a
var astatement in a block, but haven’t yet assigned a value to it, it isundefined. Again, you shouldn’t really ever need to rely on that.The spooky
typeofoperator returns'undefined'when its operand is a simple variable that does not exist, instead of throwing an error as would normally happen if you tried to refer to it. (You can also give it a simple variable wrapped in parentheses, but not a full expression involving a non-existant variable.) Not much use for that, either.This is the controversial one. When you access a property of an object which doesn’t exist, you don’t immediately get an error like in every other language. Instead you get an
undefinedobject. (And then when you try to use thatundefinedobject later on in the script it’ll go wrong in a weird way that’s much more difficult to track down than if JavaScript had just thrown an error straight away.)This is often used to check for the existence of properties:
However, because you can assign
undefinedlike any other value:that doesn’t actually detect whether the property is there reliably. Better to use the
inoperator, which wasn’t in the original Netscape version of JavaScript, but is available everywhere now:In summary,
undefinedis a JavaScript-specific mess, which confuses everyone. Apart from optional function arguments, where JS has no other more elegant mechanism,undefinedshould be avoided. It should never have been part of the language;nullwould have worked just fine for (2) and (3), and (4) is a misfeature that only exists because in the beginning JavaScript had no exceptions.Such a ‘truthiness’ test checks against
false,undefined,null,0,NaNand empty strings. But in this case, yes, it is reallyundefinedit is concerned with. IMO, it should be more explicit about that and sayif (testvar!==undefined).You can certainly assign
undefinedto it, but that won’t delete the variable. Only thedelete object.propertyoperator really removes things.deleteis really meant for properties rather than variables as such. Browsers will let you get away with straightdelete variable, but it’s not a good idea and won’t work in ECMAScript Fifth Edition’s strict mode. If you want to free up a reference to something so it can be garbage-collected, it would be more usual to sayvariable= null.Yes.