An expression is a fragment of code that produces a value. Perhaps better stated, every value that is written literally, such as a number or “word”, and followed by a semicolon, is an expression. For example: 10; is an expression that is evaluated to the numeric value 10. 10+13; is also an expression that is evaluated to produce the numeric value 23. ‘hello’ + ‘world’; evaluates to the string ‘hello world’, etc.
Imagine bindings as tentacles, rather than boxes, as bindings do not “contain” values, they “grasp” them. A program can access only the values that the binding still has a reference to (a binding points at a value), however, that does not mean it is tied to that
value forever, as simply changing an operator on existing bindings disconnect them from their current value and have them point to a new one.
let luigisDebt = 140;
luigisDebt = luigisDebt - 35;
console.log(luigisDebt);
// → 105
In the example above, the special word (keyword) let indicates that this sentence is going to define a binding, and in this example, the binding is named luigisDebt, followed by an = operator, with the given value of 140, adding the semicolon to end the expression. Though the initial binding to luigisDebt is the value 140, if luigi pays $35 towards the debt, the - operator along with the value 35 and the semicolon create a new expression, changing the binding to luigisDebt from the value 140 to a new value of 105.
The collection of bindings and their values that exist at a given time is called the environment. In a program start up, this environment is not empty, as it
always contains bindings that are part of the language standard, as well as bindings that provide ways to interact with the surrounding system. For example, a browser, has functions to interact with the currently loaded website, to read mouse and keyboard input, etc.
Values provided in the default environment have the type function, which is a piece of program wrapped in a value that can be applied (a.k.a. executed or invoked) 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 [ like this: prompt(“Enter passcode”); ].
Showing a dialog box or writing text to the screen is a side effect. A statement that changes the internal state of the application in a way that will affect the statements that come after it are called side effects. An operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, that has an observable effect besides returning a value (the main effect ) to the invoker of the operation (any application state change that is observable outside the called function other than its return value).
- Give an example of a function that produces a side effect and another function that produces a value.
Some functions produce a value, such as power:
const power = function(base, exponent) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};
console.log(power(2, 10));
… and some don’t, such as makeNoise:
const makeNoise = function() {
console.log(“Pling!”);
};
… whose only result is a side effect, as a return statement determines the value the function returns. When control comes across such a statement, it immediately jumps out of the current function and gives the returned value to the code that called the function. A return keyword without an expression after it will cause the function to return undefined, such as the makeNoise example given, which simply returned undefined.
The control flow is the order in which the computer executes statements in a script. A typical script in JavaScript includes many control structures, including conditionals, loops and functions. Parts of a script may also be set to execute when events occur. A simple example:
let theNumber = Number(prompt(“Pick a number”));
console.log("Your number is the square root of " +
theNumber * theNumber);
This example has two statements, executed from top to bottom. The first statement asks (prompts) the user for a number, and the second statement is executed after response to the first, which shows the result which is the square of that number given by the user. The function Number converts a value to a number given, as we need that conversion because the result of the prompt is a string value, and we want a number.
- What is conditional execution?
Conditional execution controls whether or not the core will execute an instruction. Prior to execution, the processor compares the condition attribute with the condition flags, if they match, then the instruction is executed, otherwise the instruction is ignored.
- What kind of keyword do you need to use to invoke conditional execution?
Conditional execution is created with the if keyword in JavaScript.
*** However, most often, it would be unlikely that you just have code that executes when a condition holds true, but would also have code that handles the other case. This alternate path is represented by the else keyword. Together with if, you create two separate, alternative execution paths.