The return value rules of arrow functions: If the function body is an expression, the expression value is returned. If the function body is a block, the last expression value in the block is returned. If the function body is empty, undefined is returned.
The return value of JavaScript arrow function
The arrow function is a kind of syntax sugar that simplifies function expressions. Introduced in ES6. Unlike traditional functions, arrow functions do not have their own this
binding and can automatically return based on an expression.
Return value rules
The return value of an arrow function is determined by the following rules:
undefined
by default. Example
<code class="javascript">// 返回表达式 const add = (a, b) => a + b; // 返回块中最后一个表达式的值 const subtract = (a, b) => { const diff = a - b; return diff; }; // 返回 undefined const noReturn = () => {};</code>
Application scenarios
Arrow functions are usually used in the following scenarios:
this
binding issues. Note:
Although arrow functions are convenient, they may not be suitable for use in the following situations:
arguments
Object. this
value. new.target
property of the constructor. The above is the detailed content of What is the return value of arrow function in js. For more information, please follow other related articles on the PHP Chinese website!