Javascript:语句和表达式的区别? [英] Javascript: difference between a statement and an expression?
问题描述
我问过这个问题在更早的思考之后,我开始怀疑术语表达和表达的含义之间似乎模糊的界限。所有的声明都是表达式? REPL控制台中的返回值来自哪里?他们并不总是表现出直观的意义。当然,如果您输入 1 + 1
,您将获得 2
,但其他时候并不是那么明显什么是逻辑。
I asked this question earlier, and after thinking about the topic some more, I began to wonder where the seemingly fuzzy boundary between the meanings of the terms "statement" and "expression" lies. Are all statements also expressions? Where do the return values in a REPL console come from? They don't always seem to make any intuitive sense. Of course if you type 1+1
, you'll get 2
, but other times it isn't as obvious what the logic is.
鉴于键入到REPL的任何内容都会产生一些值,这是否意味着可以在JS源代码中使用一个表达式和一个独立的语句?
Given that anything typed into REPL produces some value, does it mean that it can be used in JS source code as both an expression and a standalone statement?
可以在以下代码段中使用可用于 _X _
的代码字符串也可以用于 _Y _
,反之亦然 if(_X_)_Y _
can string of code that could be used for _X_
in the following snippet also be used for _Y_
and vice versa? if(_X_) _Y_
推荐答案
所有语句是否都是表达式?
Are all statements also expressions?
JavaScript可以在哪里查询一个语句,还可以写一个表达式。这样的语句称为表达式语句。反之亦然:您无法编写JavaScript期望表达式的语句。例如,if语句不能成为函数的参数。
"Wherever JavaScript expects a statement, you can also write an expression. Such a statement is called an expression statement. The reverse does not hold: you cannot write a statement where JavaScript expects an expression. For example, an if statement cannot become the argument of a function."
这是来自Axel Rauschmayer最近发表的关于这个主题的文章:
表达式与JavaScript中的语句
This is comes from a recent post by Axel Rauschmayer about this topic: Expressions versus statements in JavaScript
希望它有帮助。
这篇关于Javascript:语句和表达式的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!