Game Development Reference
In-Depth Information
are method calls and assignments, as you saw in the previous section. Instructions often use
expressions. Here are some examples of expressions:
16
numberOfBananas
2
a + 4
numberOfBananas + 12 - a
-3
"myCanvas"
All these expressions represent a value of a certain type. Except for the last line, all the expressions
are numbers. The last expression is a string (of characters). In addition to numbers and strings, there
are other kinds of expressions. I discuss the most important ones in this topic. For example, in the
following section I will discuss expressions with operators, and Chapter 7 describes using a function
or a method as an expression.
Operators and More Complex Expressions
This section talks about the different operators that JavaScript knows. You learn the priority of each
operator so you know in which order calculations are performed. You also see that sometimes,
expressions can be quite complex in JavaScript. For example, a variable can consist of multiple
values, or it can even refer to a function.
Arithmetic Operators
In expressions that are numbers, you can use the following arithmetic operators:
+ add
- subtract
* multiply
/ divide
% division remainder (pronounced “modulus”)
Multiplication uses an asterisk because the signs normally used in mathematics (∙and ×) aren't found
on a computer keyboard. Completely omitting this operator, as is also done in mathematics (for
example, in the formula fx=3x
() ), isn't allowed in JavaScript because it introduces confusion with
variables consisting of more than one character.
When the division operator / is used, in some cases the result is a real number (instead of an integer
number). For example, after executing the following instruction, the variable y contains the value 0.75:
var y = 3/4;
The special operator % gives the division remainder. For instance, the result of 14%3 is 2, and the
result of 456%10 is 6. The result always lies between 0 and the value to the right of the operator.
The result is 0 if the result of the division is an integer.
 
Search WWH ::




Custom Search