JavaScript Programming
The Assignment Statement
Commands are needed if variables are to change values, algorithms or programs. The assignment statement changes a variable's value.
There are three parts that always occur, they are listed in the following order.
- Variable - this is any declared variable in the program
- Assignment symbol - this is the equal sign (=). This is the assignments language for the operation.
- Expression - this is a formula used by the computer. This tells the computer how to calculate the new value. This is terminated by a semi-colon.
- The equal sign - (=)
- The equal sign pair - (:=)
- The left pointing arrow -
- If anything is missing from any of the three components the statement will be meaningless.
- The flow should always be from right to left. This shows that the information flows correctly.
The expression can be a variable it doesn't have to be a complex formula. - 3. The values of the variables must be the correct values before the implementation of the assignment.
An example of this would be in a football game, (totalscore = totalscore + 1;)
this shows that every goal that is scored +1 is added to the totalscore.
Assignment Symbol
All different programming languages use different symbols, but the most widely used symbols are:
Interpreting an Assignment Statement
The assignment flows from the right side (expression side) to the left hand side (variable side).
This makes using the left arrow the best option to use.
Assigned, becomes and gets bring out the whole role of the assignment.
An expression is everything to the right of the assignment symbol, this is evaluated first.
The values of the variable in this expression are taken. This then produces the new value.
The three points to remember about statements are that: