@martinrinehart said:
Does void differ from return null?
YES. (in the following, violet denotes javascript.)
(1) return type: undefined and null are separate return types.
___http://msdn.microsoft.com/en-us/library/7wkd9z69(VS.85).aspx
void(
whatever)
always returns undefined* return(
null ) obviously will return null (
in this case.)A variable can have a null value, but not an undefined value, because undefined refers to the object, not it's value. You check undefined using quotes:
%(#BF00BF)[if(typeof(var)=="undefined") //do something]
You check null without quotes:
%(#BF00BF)[if(var==null) //do something]
The difference is subtle. Basically, if a symbol references a variable that has not been declared, or has been declared but never had a value assigned, then "undefined" is returned. If a symbol references a variable that has had any value (including null,) assigned to it, it will not return "undefined".
But be careful! There's a gotcha (because of backward compatibility.)
%(#BF00BF)[(object.prop==null)] will return true if the property has the value null or if the property does not exist! You can use in to be more specific:
%(#BF00BF)[if ("prop" in object)] will return true if the property does exist.
(2) Both are evaluation functions:
return( expression ) [
called a Statement] Always returns the result (of whatever type,) that expression evaluates to.
http://msdn.microsoft.com/en-us/library/22a685h9(VS.85).aspx
void( expression ) [
called an Operator] Evaluates expression then always returns "undefined" (regardless of the result.) The expression is required or an argument error would be raised. The expression 0 is traditional, and comes from C language (where null and zero were interchangable; but in javascript, 0 is a number type, null is it's own type.)
http://msdn.microsoft.com/en-us/library/e17c7cbe(VS.85).aspx
The internal code might be represented as:
%(#BF00BF)[function void( *expression* ) {] %(#F0F0F0)[___]%(#BF00BF)[eval( *expression* );] %(#F0F0F0)[___]%(#BF00BF)[return( undefined ); }]
___