JavaScript Expressions and Statements

I was just starting to learn JavaScript using the Eloquent JavaScript book, available for free at eloquentjavascript.net. While I really like the book, there is only one section that I don’t understand. This is one of the expressions and statements:

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

I know that this topic has already been mentioned in StackOverflow before, but these were more specific questions, and I - frankly - do not understand everything.

At the beginning of the paragraph, the author explains which expressions: if I understand correctly, atomic values, such as 42 or "23", are considered expressions. If you apply these values ​​to an operator (as in 42-19), this will also be considered an expression. (I think because it obviously turns out to be 23, which is an atomic value again.) I interpret it as follows: Each value, regardless of whether it was directly entered or not yet calculated, is called an expression. It is right?

The author then says the following: “There is a unit that is larger than the expression. It is called an operator. [...] Most statements end with a semicolon (;). The simplest type of statement is an expression with a semicolon after it.” As an example, he mentions

  ! false; 
as an example. My questions are: "What does this statement do? Just a semicolon at the end?" When I use the JavaScript console, it doesn't really matter if I find it with or without a semicolon. It always returns true. However, the author says that "expression [A] only comes down to something if it somehow changes the world." So this example is not even an operator, because it "just produces [s] the value [...] true and then immediately throws [s it] into the bucket bit"? I'm really confused now ... If I hadn't completely messed it up in my head, the statement should have some “side effect” (like declaring a variable), right?

However, I would be very happy if someone could explain what the statement is. It would also be very useful if someone could give an example in which the difference between these terms is really useful, because now I can’t even imagine why the author even bothered to introduce these dictionaries. Thank you so much in advance!

+4
source share
1 answer

A simple, albeit vague, analogy would be a natural language text consisting of phrases grouped into sentences. A phrase like “rain” can make up a sentence on its own, for example, “I won’t come out, it’s raining.” or to be part of a larger offer, for example, in “Terrible weather, it rains all the time”.

However, the distinction between expressions and statements is very blurry in javascript. Unlike other C-like languages, you can have not only expressions in operators, but also expressions inside expressions:

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

Some modern javascript programs, such as jQuery, use declaration methods, which basically make them a single expression.

This blurry difference (not to mention confusion) stems from the fact that Javascript, an imperative language in the C / pascal / algol language, was at the same time heavily influenced by functional languages ​​such as Lisp, which do not have the notion of “statement” . In a functional language, everything is an expression.

To make things more interesting, the semicolon between the operators is (sometimes) optional, so there is no easy way to determine if two expressions belong to one of the operators or form two different ones. Consider:

  a = 1 // two !2 // statements a = 1 // one +2 // statement 
+4
source

All Articles