在没有return语句的情况下,javascript函数会返回什么? [英] What does javascript function return in the absence of a return statement?
问题描述
我只是想知道,没有return语句的函数(或没有命中任何return语句)是否返回一个完全等于false的值?
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?
例如:
function foo(){};
!!foo();
如果在firebug中执行,则返回false(但如果我刚调用foo()则不返回任何内容)。
This should return false if executed in firebug (but does not return anything if i just called foo();).
非常感谢!
Jason
推荐答案
没有return语句的函数(或者在没有命中的情况下结束执行的函数)将返回 undefined
。
A function without a return statement (or one that ends its execution without hitting one) will return undefined
.
如果你使用一元对于 undefined
值,否定运算符两次,您将获得 false
。
And if you use the unary negation operator twice on an undefined
value, you will get false
.
您在控制台上没有看到任何内容,因为Firebug在未定义时不打印表达式的结果(只需在控制台上输入 undefined;
,然后你什么也看不见。
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).
但是如果你打电话给 console.log
直接运行,你就可以看到它:
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'
这篇关于在没有return语句的情况下,javascript函数会返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!