[英]UnexpectedToken error when I try to use return with ternary operator but not with if statement?
so I noticed something very strange.所以我注意到一些非常奇怪的事情。 When I try to exit a function with
return
keyword in the if statement it works well but when I try to do this with the ternary operator it throws UnexpectedToken
error (expected expression, got keyword 'return').当我尝试在 if 语句中使用
return
关键字退出 function 时,它运行良好,但是当我尝试使用三元运算符执行此操作时,它会引发UnexpectedToken
错误(预期的表达式,得到关键字'return')。 Why does this happen?为什么会这样?
My code:我的代码:
function monthName(nr) {
0 < nr < 13 ? alert("Month: " + months[nr]) : return false;
}
monthName(mnr);
But with if statement:但是使用 if 语句:
function monthName(nr) {
if (0 < nr < 13)
alert("Month: " + months[nr]);
else
return false;
}
monthName(mnr);
It works fine.它工作正常。 So why can't I use
return
keyword with ternary operator but with if statement it works?那么为什么我不能将
return
关键字与三元运算符一起使用,但与 if 语句一起使用呢?
Why does this happen?
为什么会这样?
Because return
is a statement , and you can't put statements where only expressions are allowed.¹ The conditional operator's operands must be expressions, not statements.因为
return
是一个语句,你不能把语句放在只允许表达式的地方。¹条件运算符的操作数必须是表达式,而不是语句。 (You'd have the same problem with throw
, just FWIW, though there's talk of adding a throw
operator... Or with while
or for
or any other statement.) (你会遇到同样的问题
throw
,只是 FWIW,尽管有人谈论添加一个throw
运算符......或者使用while
或for
或任何其他语句。)
The if
is probably your best bet. if
可能是你最好的选择。
If you were really, really intent on just using a single expression, you could put the return
at the start so the single expression is what follows it:如果你真的很想只使用一个表达式,你可以将
return
放在开头,这样单个表达式就是它后面的内容:
function monthName(nr) {
return 0 < nr < 13 ? alert("Month: " + months[nr]) : false;
}
Note that that relies on the fact that alert
returns undefined
in order to maintain the two return values you were providing in your if
example ( undefined
if the alert
was shown, false
if not).请注意,这依赖于
alert
返回undefined
的事实,以便维护您在if
示例中提供的两个返回值(如果显示alert
,则为undefined
,否则为false
)。 Interestingly, the spec doesn't explicitly say that alert
returns undefined
but I think we can safely accept that it's implicitly specified (not least because the implicit return of any non-arrow JavaScript function is undefined
unless return <value>
is used).有趣的是,规范没有明确说明
alert
返回undefined
,但我认为我们可以安全地接受它是隐式指定的(尤其是因为任何非箭头 JavaScript function 的隐式返回是undefined
的,除非使用return <value>
)。
¹ (You can do the opposite; JavaScript has the concept of the "expression statement" which means any expression is also a valid statement.) ¹ (您可以做相反的事情;JavaScript 具有“表达式语句”的概念,这意味着任何表达式也是有效的语句。)
You can only include expressions in ternaries.您只能在三元组中包含表达式。 If you look at the MDN docs , you'll notice that there are only string, bools, etc after the condition.
如果您查看MDN 文档,您会注意到条件之后只有字符串、布尔值等。 The closest solution only returns or only alerts:
最接近的解决方案仅返回或仅警报:
function monthName(nr) {
return 0 < nr < 13 ? "Month: " + months[nr] : false;
}
or或者
function monthName(nr) {
alert(0 < nr < 13 ? "Month: " + months[nr] : false);
}
In a ternary the return
cannot be an operand.在三元中,
return
不能是操作数。 Therefore when the interpreter reads the :
operator in the ternary expression it expects a valid value to follow but instead sees the reserved keywork return
causing the program to throw and error.因此,当解释器读取三元表达式中的
:
运算符时,它期望后面跟着一个有效值,但看到的是保留的键值return
,导致程序抛出和错误。
first of all, this is not like in math, you can not do首先,这不像在数学中,你做不到
0 < nr < 13
because this will try to do the first comparison 0 < nr
and then the result of this, it will be compared to 13
因为这将尝试进行第一次比较
0 < nr
然后将其结果与13
进行比较
so basically if nr
is positive, it will always be true所以基本上如果
nr
是正数,它总是正确的
0 < nr // true
therefore所以
true < 13 // true
you can check this in the following code:您可以在以下代码中检查这一点:
console.log(0 < 5 < 13, " -> should be true. good") console,log(0 < 5000 < 13, " -> should be false! but it is true")
so in order to do the things correct, you need to concatenate the expressions using AND
所以为了做正确的事情,您需要使用
AND
连接表达式
(0 < nr) && (nr < 13)
now, to the things you asked, basically as the other responses say, you are trying to use an statement
where you can only use expressions
, if you read the documentation , it says you need to provide an expression to execute
.现在,对于您提出的问题,基本上正如其他回复所说,您正在尝试使用只能使用
expressions
的statement
,如果您阅读文档,它说您需要提供一个expression to execute
。
so, to fix your issue, you can return the whole ternary expression.因此,要解决您的问题,您可以返回整个三元表达式。
function monthName(nr) { return (0 < nr) && (nr < 13)? alert("Month: " + nr): false; } monthName(5);
or if you want a shorthand:或者如果你想要一个速记:
const monthName = nr => (0 < nr) && (nr < 13) ? alert("Month: " + nr) : false;
monthName(5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.