Espressioni e istruzioni
A fragment of code that produces a value is called an expression. Every value that is written literally (such as
"psychoanalysis") is an expression. An expression between parentheses is also an expression, as is a binary operator applied to two expressions or a unary operator applied to one.
This shows part of the beauty of a language-based interface. Expressions can nest in a way very similar to the way subsentences in human languages are nested—a subsentence can contain its own subsentences, and so on. This allows us to combine expressions to express arbitrarily complex computations.
The simplest kind of statement is an expression with a semicolon after it. This is a program:
It is a useless program, though. An expression can be content to just produce a value, which can then be used by the enclosing expression. A statement stands on its own and amounts to something only if it affects the world. It could display something on the screen—that counts as changing the world—or it could change the internal state of the machine in a way that will affect the statements that come after it. These changes are called side effects. The statements in the previous example just produce the values
true and then immediately throw them away. This leaves no impression on the world at all. When executing the program, nothing observable happens.
And that gives us our second kind of statement. The special word (keyword)
var indicates that this sentence is going to define a variable. It is followed by the name of the variable and, if we want to immediately give it a value, by an
= operator and an expression.
The previous statement creates a variable called caught and uses it to grab hold of the number that is produced by multiplying 5 by 5.
After a variable has been defined, its name can be used as an expression. The value of such an expression is the value the variable currently holds. Here’s an example:
Variable names can be any word that isn’t a reserved word (such as
var). They may not include spaces. Digits can also be part of variable names—
catch22 is a valid name, for example—but the name must not start with a digit. A variable name cannot include punctuation, except for the characters
When a variable points at a value, that does not mean it is tied to that value forever. The
= operator can be used at any time on existing variables to disconnect them from their current value and have them point to a new one.
You should imagine variables as tentacles, rather than boxes. They do not contain values; they grasp them—two variables can refer to the same value. A program can access only the values that it still has a hold on. When you need to remember something, you grow a tentacle to hold on to it or you reattach one of your existing tentacles to it.
Let’s look at an example. To remember the number of dollars that Luigi still owes you, you create a variable. And then when he pays back $35, you give this variable a new value.
When you define a variable without giving it a value, the tentacle has nothing to grasp, so it ends in thin air. If you ask for the value of an empty variable, you’ll get the value undefined.
A single var statement may define multiple variables. The definitions must be separated by commas.
Parole chiave [keywords] e riservate [reserved words]
Words with a special meaning, such as
break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield
Don’t worry about memorizing these, but remember that this might be the problem when a variable definition does not work as expected.
Qui è ancora lungo (assay), pausa 😙