Drafted:
8/12/01
Currently
being created!
COSC 330
APPENDIX B TO LM
330-VII
JAVASCRIPT KEYWORDS
AND OPERATORS
This appendix is an adjunct to Appendix
A that gives details of keywords that are so intuitive, especially to students
with experience developing software in C++ or Java. They are needed
so that the appendices to LM 330 can be complete, as far as Core Javascript
is concerned. They are not included in Appendix A because they are
intuitive and add unnecessary complexity; instead, Appendix A is simplified
by incorporating references to the details presented here.
1. CORE JAVASCRIPT KEYWORDS:
2. CORE JAVASCRIPT OPERATORS:
-
Operators take one or more variables or values
(operands) and return a new value; e.g. the '+' operator can add
two numbers to produce a third. You use operators in expressions to relate
values, whether to perform arithmetic or compare quantities. Operators
are divided into several classes depending on the relation they perform:
-
Arithmetic
operators take numerical values as operands and return a single result.
JavaScript has the standard binary arithmetic operators (+, -, *, / and
%) for addition, subtraction, multiplication, division and modulus (remander
of division).
-
Unary negation operator which returns
the negative of the operand, e.g. -5 is the negative of 5.
-
Increment and decrement operators.
ANSI STANDARD C INCREMENT AND DECREMENT OPERATORS
| ++ |
causes a unary increment,
i.e. the operand's value is increased by 1. This operator has only
one operand. The value returned depends on its orientation relative
to the operand, e.g. ++x will return the value of x after
the increment and x++ will return the value of x
before the increment. ( This
arcane notation (to me) serves no purpose other than to confuse the novice
reader (Originally, in C, is supposedly was more processor efficient.)
so I prefer to use the explicit syntax, e.g. replace x++ with x = x
+1.) |
| -- |
causes a unary increment,
i.e the reverse of ++. ( See
the preceding comment.) |
SAQ:
(a) How would one explicitly write out x++? (b) How is this useful?
-
A comparison
operator compares its two
operands
and returns a logical value based on whether the comparison is true or
not. The operands can be numerical or string values. When used on string
values, the comparisons are based on the standard lexicographical (alphabetic)
ordering.
ANSI STANDARD C COMPARISON OPERATORS
| == |
"Equal to" returns true if operands
are equal. |
| != |
"Not equal to" returns true if operands
are not equal. |
| > |
"Greater than" returns true if left
operand is greater than right operand. |
| >= |
"Greater than or equal to" returns
true if left operand is greater than or equal to right operand. |
| < |
"Less than" returns true if left
operand is less than right operand. |
| <= |
"Less than or equal to" returns true
if left operand is less than or equal to right operand. |
SAQ:
Why does JavaScript (like, C, C++, and Java) use "= =" instead of the more
sensible "=" for "equals to"?
-
Boolean operators
are typically used to combine multiple comparisons into a conditional expression.
For example, you might want to test whether (total>100) AND (stateTax=true).
A boolean operator takes two operands, each of which is a true or false
value, and returns a true or false result.
| && |
"And" returns true if both operands
are true. |
| || |
"Or" returns true if either operand
is true. |
| ! |
"Not" returns true if the negation
of the operand is true (e.g. the operand is false). |
-
Strings can be compared using the comparison
operators. Additionally, you can concatenate strings using the + operator.
| + (string concatenation) |
combines
two strings |
"Hello "
+ "World" yields "Hello World" |
JavaScript supports regular
expressions, which are defined
patterns used to match character combinations appearing in string values.
Regular expressions are very powerful, potentially allowing you to search
for any conceivable character pattern. However, they can also be quite
complex to construct. Because regular expressions are widely supported
in all high-level development environments, it is advised that you consider
learning about regular expressions as a subject unto itself. Two
detailed explanations of regular expressions can be found at
-
The assignment operator (=) lets you assign
a value to a variable. You can assign any value to a variable, including
another variable (whose value will be assigned). Several shorthand assignment
operators allow you to perform an operation and assign its result to a
variable in one step.
| = |
Assigns the value of the righthand
operand to the variable on the left.
Example: total=100;
Example: total=(price+tax+shipping) |
+=
(also -=, *=, /=) |
Adds the value of the righthand operand
to the lefthand variable and stores the result in the lefthand variable.
Example: total+=shipping (adds
value of shipping to total and assigned result to total) |
&=
(also |=) |
Assigns result of (lefthand operand
&& righthand operand) to lefthand operand. |
SAQ:
This use of "=" causes much of the strange arithmetic notation in C, C++,
Java, and JavaScript. Explain.
-
Several JavaScript operators, rarely used, fall into
no
particular category. These operators are summarized below.
Conditional operator
-
(condition) ? trueVal : falseVal
|
Assigns a specified value to a variable
if a condition is true, otherwise assigns an alternate value if condition
is false.
Example:
preferredPet = (cats > dogs) ?
"felines" : "canines"
-
If (cats>dogs), preferredPet will be assigned
the string value "felines," otherwise it will be assigned "canines".
-
NOTE: The IF-ELSE construct (See .)
can be used instead of the conditional operator. Usually this construct
is preferred because it produces more readable code.
|
| typeof operand |
Returns the data type of operand.
Example -- test a variable to determine
if it contains a number:
if (typeof total=="number") ... |
-
OPERATOR PRECEDENCE....