Three operators don't fit into any category. These are the ternary conditional operator ?:
, the binary comma operator ',
' and the binary string concatenation operator +
.
The ternary condition operator is a quick way of assigning one of two values to a variable, depending on some test. It's really just shorthand for a specific kind of if
statement (if
s are described in the next section). It works like this:
a = 1; b = 2; c = 3; d = 4;
x = ( a > b ) ? c : d; // x becomes 4
If the expression before the ?
(which should be a condition) is true
, then the expression between the ?
and the :
is assigned to x
, otherwise the expression to the right of :
is assigned to x
. This can also get quite confusing if care isn't taken.
The comma operator's main use is for passing arguments to functions (described in the next section), but technically it's a binary operator as well. What it does is evaluate its left value, throw the result away, evaluate its right value and pass that on. This might all seem useless until you realize that it allows you to put in your own side effects if you want them. Not advised, but here's an example:
a = 2; b = 3; c = 4; d = 5;
x = ( a++, c ) * ( b++, d ) // same as x=c*d; a++; b++; so why bother?
The string concatenation operator takes two String values and creates a new String which is the same as the two original strings run together. Some examples:
a = 'Red';
b = "Blue";
c = a + b + 'Yellow'; // c becomes "RedBlueYellow".
d = a + 5; // d becomes 'Red5'.
This is really useful. Since +
is already used with Numbers as the addition operator however, it can get a bit confusing. In the last line, one value is a String and one is a number, so how does +
know whether to concatenate or to add them? The section on type conversion explains, but generally you are safe provided the one value that is a string doesn't contain numeric digits. No strings are damaged in the process of concatenation—in fact, a value which is a String type is always constant—unchangeable by any means. If you want to modify a String, you have to replace it wholly with another string. When the + operator is used for string concatenation, it tries hard to perceive its two supplied values as strings.
Finally, there are some operators that don't look much like operators. They are new
, delete
, void
, and typeof()
. New
and delete
allow the creation and removal of objects, a topic covered later.
Delete is not available in Internet Explorer 3.02 or less, or Netscape 2.02. Netscape only officially supports delete in its 4.0 browser, but it won't generate an interpreter complaint if used in the 3.0 Netscape versions. In a language that is already garbage collected, delete has only a little utility.
Void
is used for controlling expressions. All expressions calculate a value, which would normally be the result of a comparison or a mathematical calculation. Sometimes it's preferable to just throw the result away, rather than storing it in a variable, particularly if the main aim of the expression is to achieve some side effect, such as changing a host object. Void
causes the expression to report undefined, rather than what the expression's result would otherwise be.
a = 5;
document.write( void (++a) ); // displays 'undefined', not 6.
Void is not available in Navigator 2.02 or Internet Explorer 3.02 or less.
Typeof
is used for identifying types. Given an expression or variable, it will report a string containing a descriptive word describing the type. See the Reference Section for the exact strings. Typeof
is one of the rare places where you can refer to a variable that has never been declared with var
without generating an error. This is especially useful since you can check for the existence of variables with typeof
.
var a = 'anything';
var b;
document.write(typeof(a)); // displays "string"
document.write(typeof(b)); // displays "undefined"
document.write(typeof(c)); // displays "undefined"
Typeof() is not available in Navigator 2.02.