4

I just started to learn JavaScript with the book "Eloquent JavaScript", which is accessible free of charge at eloquentjavascript.net. So far I really like the book, there's just one section I don't understand. It's the one about expressions and statements:

http://eloquentjavascript.net/chapter2.html#p65af5913

I know this topic has been mentioned on StackOverflow before, however, these were more specific questions and I - quite frankly - don't get the whole thing.

In the beginning of the paragraph, the author explains what expressions are: If I understand it correctly, atomic values such as 42 or "23" are being considered expressions. If one applied those values to an operator (as in 42 - 19), this would be considered an expression, too. (I guess because it obviously turns out to be 23, which is an atomic value once again.) I interpret this the following way: Every value - no matter whether it's directly typed in or is yet to be calculated - is being called an expression. Is that correct?

Then the author says the following: "There exists a unit that is bigger than an expression. It is called a statement. [...] Most statements end with a semicolon (;). The simplest kind of statement is an expression with a semicolon after it." As an example he mentions

!false;
as an example statement. My questions is "What makes this a statement? Just the semicolon at the end?" When I use the JavaScript console, it makes absolutely no difference whether I type that in with or without the semicolon. It always returns true. However, the author says that "[A] statement only amounts to something if it somehow changes the world." So the given example is not even a statement since it "just produce[s] the value [...] true, and then immediately throw[s it] into the bit bucket"? I am really confused now... If I did not entirely mess it up in my head, a statement has to have some "side-effect" (like a declaration of a variable), right?

However, I would be very happy if someone could explain what a statement is. It would also be very helpful if someone could give an example in which the distinction of those terms is actually useful, because right now I cannot even imagine why the author even bothers to introduce these vocabularies. Thank you very much in advance!

typeduke
  • 6,494
  • 6
  • 25
  • 34
  • Yes, every value of any type is, on its own, an expression. One type of statement is an expression too, and it's not that the semicolon *makes it* a statement; it's an expression and a statement at the same time. (The semicolon is not part of the expression however.) – Pointy Jan 15 '13 at 22:23
  • A statement is just a syntactic construct anyway. When the author talks about a statement "amounting to something", it's an informal joke. A statement that does nothing (like `false;`) is still a statement. – Pointy Jan 15 '13 at 22:25
  • You can just see it as that expressions are a subset of statements, because every expression is also an *expression statement*. Or does this confuse you even more? ;) – Felix Kling Jan 15 '13 at 22:25
  • 1
    Oh, and finally the reason it's good to know what the terms mean is that they come up when describing other syntactic constructs. That is, there are times when something is being described, and knowing that you can plug in "any expression" or "any statement" makes a significant difference. – Pointy Jan 15 '13 at 22:27
  • @FelixKling So every expression is also a statement. So there must me a statement that is not an expression at the same time, I guess. What would that look like? – typeduke Jan 15 '13 at 22:29
  • @Pointy So could you explain this informal joke to me? If a statement does not necessarily "change the world", just like an expression, why would you consider it to be something different than a statement. – typeduke Jan 15 '13 at 22:31
  • Thank you guys for your quick responses so far :D – typeduke Jan 15 '13 at 22:32
  • Any control structures, such as `if` *statements*, `for`, `foreach`, `while` loops are all statements. A function *declaration* is a statement too. A variable declaration with `var` is a statement. – Felix Kling Jan 15 '13 at 22:35

1 Answers1

4

A simple, albeit vague, analogy would be a text in a natural language, consisting of phrases grouped into sentences. A phrase, like "it's raining" can form a sentence by itself, like "I won't go out. It's raining." or be a part of a bigger sentence, like in "Terrible weather, it's raining all the time."

That said, the distinction between expressions and statements is very blurred in javascript. Unlike other C-alike languages, you can have not only expressions in statements, but also statements inside expressions:

a = 1 + function(x) { if(x > 1) return 10 } (20) 

Some modern javascript programs, such as Jquery, use declaration techniques that basically make them one single expression.

This blurry distinction (not to say confusion) comes from the fact that Javascript, being a C/pascal/algol-like imperative language, was at the same time heavily influenced by functional languages like Lisp, which don't have a notion of "statements". In a functional language, everything is an expression.

To make things more interesting, the semicolon between statements is (sometimes) optional, so that there's no easy way to tell if two expressions belong to one statement or form two distinct ones. Consider:

 a = 1  // two
 !2     // statements

 a = 1  // one
 +2     // statement
georg
  • 211,518
  • 52
  • 313
  • 390
  • I would disagree with statements being possible inside expressions. Just go through the list of expressions (http://es5.github.com/#x11-toc) and see whether any of those allows statements. There are constructs which can interpreted as either an expression or a statement, but otherwise, the distinction is quite clear. – Felix Kling Jan 15 '13 at 22:49
  • @FelixKling: added an example. – georg Jan 15 '13 at 22:52
  • Ah... mmh ok. I guess function expressions are somehow "special" ;) (which is somehow supported by the fact that they have their own section in the specification). – Felix Kling Jan 15 '13 at 22:53
  • @thg435 Thank you for your natural language example. If "I won't go out. It's raining." represents a statement, though, I don't think this could be part of a even bigger phrase group or sentence. So this example doesn't quite work with the idea that statements can also be part of expressions. ;) – typeduke Jan 15 '13 at 23:04
  • @duebelmann: Consider: `" 'I won't go out. It's raining.' says Julia".` – georg Jan 15 '13 at 23:08
  • Concerning the example you added, please let me know if I interpret it correctly. You claim this whole construct to be an expression, even though a value is being assigned to the variable a. (Which I thought would make it a statement) Doesn't matter, let's say this is an expression. What you then do is you declare a function and immediately afterwards you call it by passing it the value 20. Is that interpreted correctly? I didn't know that this was even possible, however, this would actually be a function declaration/call (which is considered a statement) within an expression. Am I right? – typeduke Jan 15 '13 at 23:10
  • @duebelmann as in many other languages, the various assignment operators are in fact just operators in the expression grammar. A single expression can involve many assignments to variables (or object properties). – Pointy Jan 15 '13 at 23:19
  • @duebelmann: to make the long story short, Javascript is a messy language. In a matter of fact, it's very messy. If you're serious about learning it, be prepared to discover _lots_ of weird things both in syntax and in semantics. – georg Jan 15 '13 at 23:26
  • I just got reminded of this answers somehow: http://stackoverflow.com/a/347435/218196 :) – Felix Kling Jan 15 '13 at 23:30