An expression is a piece of code that produces a value, which could be a simple string or number, literally, or something more complex like a nested set of expressions.
Bindings are ways to grasp values. Reserved words âvarâ, âletâ, or âconstâ will allow you to assign values to a placeholder that you get to name. The construct would be âkeyword placeholder-name = some valueâ, for example, var firstName = âIvanâ. Placeholder names cannot begin with a number, nor can they be any of the reserved keywords of javascript, they can contain â$â or â_â but no other special character, and they should be descriptive - it makes reading code and debugging oh such much easier. Var and let are used to assign variables while const is used to assign constants that cannot be reassigned in the same program.
The environment is the sum of all bindings and their values. This would include functions that exist in javascript to interact with a browser. An interactive webpage environment changes as the user interacts with the page, so environments arenât static. An example of a function that is always present in a javascript environment is Number.isNaN, which tests whether the argument passed to it is ânot a numberâ.
Functions are bits of code wrapped by values. To construct a function, take a bit of code and wrap it in curly braces { }, making sure each line of code ends with a semi-colon. Precede the { } with the reserved keyword âfunctionâ followed by a name that you select for your function, followed by opening and closing parentheses, ( ). Inside the parentheses is the magic spot! Thatâs where we can put a variable or other values to pass to the function. Getting a function to run is called invoking, calling or applying the function. Invoke a function by calling its name and perhaps passing a couple of values. Calling a function will return some value that was created by the wrapped bit of code.
For example, a function called âwaterGardenâ could be used to alert someone to do that chore when the predicted high temperature for the day is 85F or higher. It might look like:
function waterGarden (highTemp) {
let chores = null;
if (highTemp > 85) {
chores = âOver 85 today â Water Garden This Morning!â;
}
else {
chores = âCool day â Walk The Dog!â;
}
return chores;
}
The function would be called like this: waterGarden(87) or waterGarden(75) and we can output the returned value of chores using console.log, another function always available by default.
console.log(waterGarden(87));
console.log(waterGarden(75));
Functions return values but can have side effects like presenting output on a screen.
Control flow refers to the order in which lines of code are executed. A program is read from the top down line by line unless a conditional expression diverts the flow. Certain reserved keywords, like âifâ, âelse ifâ, âelseâ, and âbreakâ change the flow based on some evaluated condition.