Every function returns a value, which by default is undefined
.
每个函数都返回一个值,默认情况下为undefined
。
Any function is terminated when its lines of code end, or when the execution flow finds a return
keyword.
当任何函数的代码行结束或执行流找到return
关键字时,该函数将终止。
When JavaScript encounters this keyword it exits the function execution and gives control back to its caller.
当JavaScript遇到此关键字时,它将退出函数执行并将控制权交还给其调用者。
If you pass a value, that value is returned as the result of the function:
如果传递一个值,则该值作为函数的结果返回:
const dosomething = () => {
return 'test'
}
const result = dosomething() // result === 'test'
You can only return one value.
您只能返回一个值。
To simulate returning multiple values, you can return an object literal, or an array, and use a destructuring assignment when calling the function.
为了模拟返回多个值,您可以返回对象常量或数组 ,并在调用函数时使用解构分配。
Using arrays:
使用数组:
Using objects:
使用对象: