I was just wondering, does a function without a return statement (or without hitting any return statements) return a value that is completely equivalent to false?
For example:
function foo(){}; !!foo();
This should return false if executed in firebug (but does not return anything if i just called foo();).
Thanks a lot!
Jason
If no return value is specified, JavaScript will return undefined . In many languages the result of the last statement is returned (more useful, IMO). These are called implicit returns.
Usually, the JavaScript functions returns a single value, but we can return multiple values by using the array or object. To return multiple values, we can pack the values as the object's properties or array elements.
"Blank return" statements can be used to transfer the control back to the calling function (or stop executing a function for some reason - ex: validations etc). In most cases I use blank return statement is when I'm doing some kind of a validation.
Function Return When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
A function without a return statement (or one that ends its execution without hitting one) will return undefined
.
And if you use the unary negation operator twice on an undefined
value, you will get false
.
You are not seeing anything on the console because Firebug doesn't prints the result of an expression when it's undefined (just try typing undefined;
at the console, and you will see nothing).
However if you call the console.log
function directly, and you will be able to see it:
function foo(){} console.log(foo()); // will show 'undefined'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With