Programming with JavaScript
A SCRIPT IS A SERI ES OF INSTRUCTIONS
A script is a series of instructions that a computer can follow to achieve a goal. You could compare scripts to any of the following:
- RECIPES
- HANDBOOKS
- MANUALS
WRITING A SCRIPT
To write a script, you need to first state your goal and then list the tasks that need to be completed in order to achieve it. Humans can achieve complex goals without thinking about them too much, for example you might be able to drive a car, cook breakfast, or send an email without a set of detailed instructions. But the first time we do these things they can seem daunting.
Therefore, when learning a new skill, we often break it down into smaller tasks, and learn one of these at a time. With experience these individual tasks grow familiar and seem simpler.
Some of the scripts you will be reading or writing when you have finished this book will be quite complicated and might look intimidating at first. However, a script is just a series of short instructions, each of which is performed in order to solve the problem in hand. This is why creating a script is like writing a recipe or manual that allows a computer to solve a puzzle one step at a time.
**Start with the big picture of what you want to achieve, and break that down into smaller steps. **
1: DEFINE THE GOAL First, you need to define the task you want to achieve. You can think of this as a puzzle for the computer to solve.
2: DESIGN THE SCRIPT To design a script you split the goal out into a series of tasks that are going to be involved in solving this puzzle. This can be represented using a flowchart. You can then write down individual steps that the computer needs to perform in order to complete each individual task (and any information it needs to perform the task), rather like writing a recipe that it can follow.
3: CODE EACH STEP Each of the steps needs to be written in a programming language that the compu ter understands. In our case, this is JavaScript.
EXPRESSIONS
An expression evaluates into (results in) a single value. Broadly speaking there are two types of expressions.
OPERATORS
Expressions rely on things called operators; they allow programmers to create a single value from one or more values.
ARITHMETI C OPERATORS
JavaScript contains the following mathematical operators, which you can use with numbers. You may remember some from math class.
WHAT IS A FUNCTION?
Functions let you group a series of statements together to perform a specific task. If different parts of a script repeat the same task, you can reuse the function (rather than repeating the same set of statements).
A BASIC FUNCTION
In this example, the user is shown a message at the top of the page. The message is held in an HTML element whose id attribute has a value of message. The message is going to be changed using JavaScript. Before the closing </body> tag, you can see the link to the JavaScript file. The JavaScript file starts with a variable used to hold a new message, and is followed by a function called updateMessage(). You do not need to worry about how this function works yet - you will learn about that over the next few pages. For the moment, it is just worth noting that inside the curly braces of the function are two statements. These statements update the message at the top of the page. The function acts like a store; it holds the statements that are contained in the curly braces until you are ready to use them. Those statements are not run until the function is called. The function is only called on the last line of this script.