What is an expression?
A fragment of code that produces a value is called an expression. Every value
that is written literally (such as 22 or â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.
If an expression corresponds to a sentence fragment, a JavaScript statement
corresponds to a full sentence. A program is a list of statements.
The simplest kind of statement is an expression with a semicolon after it.
What is a binding?
To catch and hold values, JavaScript provides a thing called a
binding, or variable:
let caught = 5 * 5;
You should imagine bindings as tentacles, rather than boxes. They do not
contain values; they grasp themâtwo bindings can refer to the same value
Example:
let luigisDebt = 140;
luigisDebt = luigisDebt - 35;
console.log(luigisDebt);
// â 105
What is an environment?
The collection of bindings and their values that exist at a given time is called
the environment. When a program starts up, this environment is not empty. It
always contains bindings that are part of the language standard, and most of the
time, it also has bindings that provide ways to interact with the surrounding
system.
What is a function?
A function is a piece of program wrapped in a value. Such values can be applied
in order to run the wrapped program.
Give an example of a function.
For example, in a browser environment,
the binding prompt holds a function that shows a little dialog box asking for
user input. It is used like this:
prompt(âEnter passcodeâ);
What is a side effect? Showing a dialog box or writing text to the screen is a side effect.
Give an example of a function that produces a side effect and another function that produces a value.
Side effect function: prompt(âEnter passcodeâ);
Value Producing Function:
console.log(Math.max(2, 4));
// â 4
What is control flow? The control flow is the order in which the computer executes statements in a script
When your program contains more than one statement, the statements are
executed as if they are a story, from top to bottom. Below is an example
let theNumber = Number(prompt(âPick a numberâ));
console.log("Your number is the square root of " +
theNumber * theNumber);
What is conditional execution?
Not all programs are straight roads. We may, for example, want to create
a branching road, where the program takes the proper branch based on the
situation at hand. This is called conditional execution.
What kind of keyword do you need to use to invoke conditional execution?
If.
Conditional execution is created with the if keyword in JavaScript. In the
simple case, we want some code to be executed if, and only if, a certain condition
holds. We might, for example, want to show the square of the input only if the
input is actually a number.
let theNumber = Number(prompt(âPick a numberâ));
if (!Number.isNaN(theNumber)) {
console.log("Your number is the square root of " +
theNumber * theNumber);
}