Why JavaScript functions always return a value?

前端 未结 4 1865
傲寒
傲寒 2020-11-27 03:41

I\'m taking a course in JavaScript programming, and the instructor said that a typical JavaScript function always returns a value. Even when we don\'t provide any explicit r

4条回答
  •  醉话见心
    2020-11-27 04:28

    That's by ECMAScript specification

    13.2.1 [[Call]] ... 6. Otherwise result.type must be normal. Return undefined.

    Essentially any JS function is compiled as if it has implicit return undefined; at the end:

    function foo() {
      ...
    
      return undefined; // implicit non-visible statement  
    }
    

提交回复
热议问题