In the example we just saw, the one line is a statement.
A program consists of statements.
This statement has a function call. console.log
is a function. "hello, world" is a literal
constant, in this case a string.
The console.log function prints.
The literal constant is an argument
to the function, we say it is a message to the function
about what we want it to print.
The following is another statement.
This statement consists of several expressions .
42
42 is
an expression, a literal value expression.
bar
bar is a variable expression,
colloquially a variable,
which, when used, returns the current value of the variable.
bar + 42
+.
let foo = bar + 42
let is a
keyword of JavaScript. This keyword
is used whn we want to declare,
create, a variable.
foo is a variable
expression being assigned the value of the arithmetic
expression mentioned above. The two expressions are joined
by the assignment operator
=.
The way the computer treats a line of code, a statement, is that it processes the codeline left to right. If it is an assignment, it starts immediately to the right of the "=" and then proceeds working towards the right. Again, if it is an assignment, the processed right hand side results in a value, which is then assigned to the variable at the lefthand sign of the equals operator.
In case an expression contains parenthesis, the innermost will be executed first, then the next, working outwards just like in arithmetic.
Definition: A program, a script, is a series of statements.
A variable name must start with an alphabetic character.
The name contains letters or digits after the first
character.
Variable names should be meaningful with respect to what
they are used for. If a program is about potatoes and they
must be counted, count, or perhaps
potatoCount might be good variable
names[3].
We are dealing with JavaScript in this course, so let us turn to a browser for exemplification.
Press Ctrl-(shift)I and click console. Then we key in the following pressing enter when we see an ↵
bar = 2↵
2
foo = bar + 40↵
42
[3] If ever at loss for a variable name, programmer folklore uses the so called metasyntactic variable names. They are meaningless, but funny. Look them up at https://en.wikipedia.org/wiki/Metasyntactic_variable in Wikipedia, or perhaps in http://tools.ietf.org/html/rfc3092, RFC 3092 - Etymology of "Foo". They offer an impressive list of names: foo, bar, baz, qux, quux, corge, grault, garply, waldo, fred, plugh, xyzzy, and thud, not to mention spam, ham, and eggs.