4.19 Binary Operators
The subsections that follow specify the compile-time processing rules of the binary operators. In general, if the operands of a binary operator do not meet the stated requirements, a compile-time error occurs and the result of the operation defaults to type any in further processing. Tables that summarize the compile-time processing rules for operands of the Any type, the Boolean, Number, and String primitive types, and all other types (the Other column in the tables) are provided.
4.19.1 The *, /, %, –, <<, >>, >>>, &, ^, and | operators
These operators require their operands to be of type Any, the Number primitive type, or an enum type. Operands of an enum type are treated as having the primitive type Number. If one operand is the null
or undefined
value, it is treated as having the type of the other operand. The result is always of the Number primitive type.
Any | Boolean | Number | String | Other | |
---|---|---|---|---|---|
Any | Number | Number | |||
Boolean | |||||
Number | Number | Number | |||
String | |||||
Other |
TODO: Document the exponentation operator.
4.19.2 The + operator
The binary + operator requires both operands to be of the Number primitive type or an enum type, or at least one of the operands to be of type Any or the String primitive type. Operands of an enum type are treated as having the primitive type Number. If one operand is the null
or undefined
value, it is treated as having the type of the other operand. If both operands are of the Number primitive type, the result is of the Number primitive type. If one or both operands are of the String primitive type, the result is of the String primitive type. Otherwise, the result is of type Any.
Any | Boolean | Number | String | Other | |
---|---|---|---|---|---|
Any | Any | Any | Any | String | Any |
Boolean | Any | String | |||
Number | Any | Number | String | ||
String | String | String | String | String | String |
Other | Any | String |
A value of any type can converted to the String primitive type by adding an empty string:
function getValue() { ... }
var s = getValue() + "";
The example above converts the result of ‘getValue()’ to a string if it isn’t a string already. The type inferred for ‘s’ is the String primitive type regardless of the return type of ‘getValue’.
4.19.3 The <, >, <=, >=, ==, !=, ===, and !== operators
These operators require one or both of the operand types to be assignable to the other. The result is always of the Boolean primitive type.
Any | Boolean | Number | String | Other | |
---|---|---|---|---|---|
Any | Boolean | Boolean | Boolean | Boolean | Boolean |
Boolean | Boolean | Boolean | |||
Number | Boolean | Boolean | |||
String | Boolean | Boolean | |||
Other | Boolean | Boolean |
4.19.4 The instanceof operator
The instanceof
operator requires the left operand to be of type Any, an object type, or a type parameter type, and the right operand to be of type Any or a subtype of the ‘Function’ interface type. The result is always of the Boolean primitive type.
Note that object types containing one or more call or construct signatures are automatically subtypes of the ‘Function’ interface type, as described in section 3.3.
4.19.5 The in operator
The in
operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, and the right operand to be of type Any, an object type, or a type parameter type. The result is always of the Boolean primitive type.
4.19.6 The && operator
The && operator permits the operands to be of any type and produces a result of the same type as the second operand.
Any | Boolean | Number | String | Other | |
---|---|---|---|---|---|
Any | Any | Boolean | Number | String | Other |
Boolean | Any | Boolean | Number | String | Other |
Number | Any | Boolean | Number | String | Other |
String | Any | Boolean | Number | String | Other |
Other | Any | Boolean | Number | String | Other |
4.19.7 The || operator
The || operator permits the operands to be of any type.
If the || expression is contextually typed (section 4.23), the operands are contextually typed by the same type. Otherwise, the left operand is not contextually typed and the right operand is contextually typed by the type of the left operand.
The type of the result is the union type of the two operand types.
Any | Boolean | Number | String | Other | ||||
---|---|---|---|---|---|---|---|---|
Any | Any | Any | Any | Any | Any | |||
Boolean | Any | Boolean | N | B | S | B | B | O |
Number | Any | N | B | Number | S | N | N | O |
String | Any | S | B | S | N | String | S | O |
Other | Any | B | O | N | O | S | O | Other |