A fragment of code that produces a value is called an expression.
To catch and hold values, JavaScript provides a thing called a binding, or variable.
The collection of bindings and their values that exist at a given time is called the environment. For example, in a browser there are functions to interact with the currently loaded website adn to read mouse and keyboard input.
A function is a piece of program wrapped in a value. Such values can be applied in order to run a wrapped program. A clear example of a function is a dialogue box in a browser setting prompting the user to enter a passcode.
Showing a dialog box or writing text to the screen is a side effect. For example, Math.max takes any amount of number arguments and gives back the greatest.
console.log(Math.max(2, 4));
// â 4
Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain)
- Logging to the console.
- Writing to the screen.
console.log(Math.max(2, 4));
// â 4
- Writing to a file.
- Writing to the network.
- Triggering any external process.
- Calling any other functions with side-effects.
The control flow is the order in which the computer executes statements in a script. A typical script in JavaScript or PHP (and the like) includes many control structures, including conditionals, loops and functions. Parts of a script may also be set to execute when events occur.
Code is run in order from the first line in the file to the last line, unless the computer runs across the (extremely frequent) structures that change the control flow, such as conditionals and loops.
For example, imagine a script used to validate user data from a webpage form. The script submits validated data, but if the user, say, leaves a required field empty, the script prompts them to fill it in. To do this, the script uses a conditional structure or ifâŚelse, so that different code executes depending on whether the form is complete or not:
if (field==empty) {
promptUser();
} else {
submitForm();
}
The if/else statement is a part of JavaScriptâs âConditionalâ Statements, which are used to perform different actions based on different conditions.
The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
2021-04-27T02:44:00Z