What does javascript function return in the absence of a return statement?

前端 未结 3 1695
别跟我提以往
别跟我提以往 2020-11-27 05:15

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?

Fo

相关标签:
3条回答
  • 2020-11-27 05:51

    to find out, try this in firebug console:

    alert((function(){})());
    
    0 讨论(0)
  • 2020-11-27 06:12
    <html>
    <body>
    <script>
    function a() {}
    alert(a());
    </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 06:18

    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'
    
    0 讨论(0)
提交回复
热议问题