JavaScript isn't really a typed language. This means that you don't have to worry much about what kind of thing you are dealing with. We don't have to do anything special to set or get values from a text field depending on whether they are numbers or characters.
Sometimes we need a place to hold a value. We might need a copy so we can change something and then change it back. Or we might need to hold onto part of a calculation while we complete other parts. Or it might represent something we need later, like a window we might create or a picture. To do this, we can create variables. These are little boxes that can hold any value. But they can only hold one value at a time. You make a variable like this.
The scope of a variable is where it can be seen. That is, which parts of the program can see the variable. In JavaScript, we have essentially two scopes. Global variables can be seen anywhere on the web page that contains it. This is like having a sign on the outside of a business. Everybody on the street can see it. But the business next door can't see the signs on the inside. These inside signs are what local variables are for. They are not visible outside the function they are defined in. Only code inside the function can set or get them.
Variables can be used anywhere it makes sense to put their value. So, you can use a variable that contains a number anywhere you could put a number. Like in arithmetic, comparisons, etc. Same with variables that hold strings.
Expressions can be just a variable by itself or they can be variables, numbers and operators. There are two main classes of operators, arithmetic and logical. Arithmetic operators include
+ | addition |
---|---|
- | subtraction |
/ | division |
* | multiplication |
% | modulo (remainder) |
== | is equal | |
---|---|---|
!= | is not equal | |
< | is less than | |
> | is greater than | |
>= | is greater than or equal to | |
<= | is less than or equal to | |
&& | and | |
|| | or |
Assignment copies a value from one variable to another or sets a variable to a value. The operator for this is a single =. Some examples are:
This is different than how people do this. If somebody gives you a quarter, you just put it in your pocket. The computer equivalent is to take all the coins out of your pocket, put the quarter in the pile and put them all back into your pocket.
There are some short cuts available for common operations
a++; | a = a + 1; | |
---|---|---|
a--; | a = a - 1; | |
a += 3; | a = a + 3; | |
a -= 3; | a = a - 3; | |
a *= 3; | a = a * 3; | |
a /= 3; | a = a / 3; |
Functions are a named group of instructions. You can give the group some values to work with and they can send back a result. The values you send in are called parameters or arguments. They are treated like a local variable and you can have as many as you want. Functions can only send back or return a single value. Function names can be any variable name.
Functions have to be defined before you can use them. A definition looks like this
The purpose of a conditional statement is to alter the flow of control. What that means is that you can do different things depending on the conditions. The whole thing looks like:
You can also nest the statements like this.
Another way to alter program flow is to use a switch statement. This works pretty much the same as a nested if-else complex, but is much easier to read.
Many times you want the same code to be run several times. This is different than a function. You might want to run the same code several times in a row. JavaScript supports several ways to do this. The while loop uses an expression to control how many times the code is run. It keeps running the code until the expression is false. The general syntax is:
If you want the code to run at least once, use a do loop. In a while loop, the expression is checked before the code is run. In a do loop, it is checked after.
The last loop structure is best if there is a fixed number of times you want the code to run. The for loop is given a start value, a check for when it is done and how to change the value.
The loops can replace each other. For example, the loop above could be written as a while loop.