A fundamental skill in programming is controlling how a program executes. The control of program execution is categorized using three control structures. They are "sequential", "selection", and "repetition". So far, you've written straightforward code that uses sequential JavaScript statements that execute one after another. More complex decision-making programs use selection and repetition. Programs also implement different types of looping structures, such as looping until some condition is met (sentinel) or looping a specific number of times (counter control). As you read through this section, you should notice that control structures in JavaScript use "reserved keywords" (if, else, switch).
let theNumber = Number(prompt("Pick a number"));
console.log("Your number is the square root of " +
theNumber * theNumber);
The function Number
converts a value to a number. We need that conversion because
the result of prompt
is a string value, and we want a number. There are similar functions called String
and Boolean
that convert values to those types.
Here is the rather trivial schematic representation of straight-line control flow:
Source: Marijn Haverbeke, https://eloquentjavascript.net/02_program_structure.html
This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.